Cover image for Interview Classic 150 Questions P215 Kth Largest Element in an Array

Interview Classic 150 Questions P215 Kth Largest Element in an Array


Timeline

timeline

2025-12-05

init

quick sort, heap

Title:

heap sort

forcomplete binary treenumber the nodes, with the following relationship

Item1-based numbering(root=1)0-based indexing(root=0)
left childleft(i)=2i\text{left}(i)=2i left(i)=2i+1 \text{left}(i)=2i+1
right childright(i)=2i+1 \text{right}(i)=2i+1 right(i)=2i+2 \text{right}(i)=2i+2
parent nodeparent(i)=i/2i>1 \text{parent}(i)=\lfloor i/2 \rfloor(i>1)parent(i)=(i1)/2i>0 \text{parent}(i)=\lfloor (i-1)/2 \rfloor(i>0)
condition for having at least a left child2in 2i \le n 2i+1<n 2i+1 < n
condition for existence of right child2i+1n 2i+1 \le n 2i+2<n 2i+2 < n
leaf node conditioni>n/2i > \lfloor n/2 \rfloori>(n2)/2 i > \lfloor (n-2)/2 \rfloor or2i+1n2i+1 \ge n
condition for having only left child2i=n2i = n 2i+1<n 2i+1 < n and 2i+2n 2i+2 \ge n
depth (root level=1)log2i+1 \lfloor \log_2 i \rfloor +1 log2(i+1)+1 \lfloor \log_2 (i+1) \rfloor +1
depth (root level=0)log2i \lfloor \log_2 i \rfloor log2(i+1) \lfloor \log_2 (i+1) \rfloor

Heap sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <vector>
using std::vector;

class Solution {
private:
void heapify(vector<int> &vec, int curr, int n)
{
int left = curr * 2 + 1;
int right = curr * 2 + 2;
int largest = curr;

if (left < n && vec[left] > vec[largest]) {
largest = left;
}

if (right < n && vec[right] > vec[largest]) {
largest = right;
}

if (largest != curr) {
std::swap(vec[largest], vec[curr]);
//Since the parent and child nodes have been swapped, it may affect the subtree of the child node, so adjust the subtree of the child node.
heapify(vec, largest, n);
}
}

void buildMaxHeap(vector<int> &vec)
{
int n = vec.size();
// The position of the last node is n-1, so the position of the parent node is (n-1-1)/2.
for (int i = (n - 2) / 2; i >= 0; i--) {
heapify(vec, i, n);
}
}

public:
int findKthLargest(vector<int> &nums, int k)
{
int n = nums.size();
buildMaxHeap(nums);

for (int i = 0; i < k - 1; i++) {
// pop
std::swap(nums[0], nums[n - 1]);
n--;
heapify(nums, 0, n);
}
return nums[0];
}
};

Quick sort

Note that the Hoare partition scheme does not care where elements equal to the pivot are, only requires left <= pivot and right >= pivot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <vector>
using std::vector;
class Solution {
private:
int quick_sort(vector<int> &nums, int start, int end, int k)
{
if (end <= start) {
return nums[k];
}
int pivot = nums[(start + end) / 2];
int left = start - 1;
int right = end + 1;
while (left < right) {
do {
left++;
} while (nums[left] < pivot);
do {
right--;
} while (nums[right] > pivot);
if (left < right) {
std::swap(nums[left], nums[right]);
}
}
// start..=right, right+1..=end
if (k<= right) {
return quick_sort(nums, start, right, k);
} else {
return quick_sort(nums, right+1, end, k);
}
}

public:
int findKthLargest(vector<int> &nums, int k)
{
int n = nums.size();
// The K-th largest is the (n-k)-th smallest.
return quick_sort(nums, 0, n - 1, n - k);
}
};