The most intuitive solution to this problem is to sort the array first, then return the K element, but the time complexity of the method is O (Nlog (n)), higher. There are two ways to compare colleges and universities: one is the idea of heap sequencing, and the other is the idea of quick shooting.
first, the idea of a heap sort. the idea is to set the small top heap of K, and compare the remaining elements with the top elements of the heap, and if the element is smaller than the top element of the heap, it will not be the element of K, directly processing the next element, if the heap top element is larger than the top of the heap, it is impossible to replace the heap top element with the element The time complexity for each heap adjustment is O (log (k)), with a total of n elements, then the total time complexity of the idea is: O (n (log (k))).
int Findkthlargest (vector<int>& nums, int k) {int len = nums.size (); int i = (k-1-1)/2; int tmp; Adjust the initial heap while (I >= 0) {int tmpi = i; TMP = Nums[i]; Int J = i*2 + 1; while (J < K) {if (J+1 < K && Nums[j+1] < nums[j]) J + +; if (Nums[j] >= tmp) break; Nums[i] = Nums[j]; i = j; j = i*2 + 1; } Nums[i] = tmp; i = tmpi-1; }//traverse the remaining elements for (int l = k; l < Len; l++) {if (Nums[l] <= nums[0]) continue; Nums[0] = nums[l]; TMP = Nums[0]; i = 0; Int J = i*2 + 1; while (J < K) {if (j + 1 < k && Nums[j + 1] < Nums[j]) J + +; if (Nums[j] >= tmp) break; NumS[i] = Nums[j]; i = j; j = i*2 + 1; } Nums[i] = tmp; } return nums[0]; }
Second, the idea of quick-line. This idea is processed according to the idea of fast sequencing, first select the array first element A into a quick-row processing, so that smaller than its elements in front of its larger than its elements are behind it, after processing if a at this time position in the K position, then directly returned, otherwise, if smaller than k, then the elements after the same treatment, if the ratio of K The same processing of the previous elements. The position of the element where it was processed is returned at the position of the K element. In the worst case scenario, half of the data needs to be sorted, and the time complexity is O (n (log (n))), but in general the time complexity is much smaller.
//Quick Line idea int Kthlargest (vector<int>& nums, int i, int j, int k) {int start = i; int end = J; int tmp; while (Start < end) {TMP = Nums[start]; while (Start < end && Nums[end] < tmp) end--; Nums[start] = Nums[end]; Nums[end] = tmp; while (Start < end && Nums[start] > tmp) start++; Nums[end] = Nums[start]; Nums[start] = tmp; } if (start = = k-1) return Nums[start]; else{if (Start < k-1) {return kthlargest (nums, start + 1, J, K); }else{return Kthlargest (Nums, I, start-1, k); }}} int findkthlargest (vector<int>& nums, int k) {int len = nums.size (); Return Kthlargest (nums, 0, len-1, K); }<span id= "_xhe_cursor" ></SPAN>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Kth largest Element in an Array