备忘,参考:点击打开链接
1. Simple Solution:A Simple Solution is to sort the given array using a O(nlogn) sorting algorithm like Merge Sort, Heap Sort, etc and return the element at index k-1 in the sorted array. Time Complexity of this solution is O(nLogn).
2. Using Min Heap – HeapSelect:create a Min Heap of the given n elements and call extractMin() k times. Time complexity of this solution is O(n + kLogn).
3. Using Max-Heap :
1) Build a Max-Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k)
2) For each element, after the k’th element (arr[k] to arr[n-1]), compare it with root of MH.
……a) If the element is less than the root then make it root and call heapify for MH
……b) Else ignore it.
// The step 2 is O((n-k)*logk)
3) Finally, root of the MH is the kth smallest element.
Time complexity of this solution is O(k + (n-k)*Logk)
4. QuickSelect:t. The worst case time complexity of this method is O(n2), but it works in O(n) on average.