Introduction
Quick select is a transformation of quicksort, we can use it to find the $k-th$ element in a list if it exists. The average time complexity of this algorithm is $O(n)$. We all know that the average time complexity of quick sort is $O(nlogn)$.
This kind of problem can also be solved by using Max_heap or Min_heap, the time complexity is $O(nlogk)$.
Quick Select
Input: array nums, int k. (find k-th smallest element in an unsorted array)
Output: int target
- Choose an element from the array as pivot, exchange the position of pivot and number at the end of the array.
- The pivot can either be the end element or a random chosen element. A random chosen pivot can make the algorithm much possibly run in average case time.
- Partition the array into 2 parts in which the numbers in left subarray is less than (or equal to) the pivot and the numbers in right subarray is greater than (or equal to) the pivot.
- Exchange pivot (at the end of the array now) with the first element in the right part.
- Compare k with length of the left subarray, say, len.
- if
k == len + 1, then pivot is the target.- if
k <= len, repeat from step 1 on the left subarray.- if
k > len, k = k - len, repeat from step 1 on the right subarray.
implementation
| 1 | #快速选择 找到第(k+1)小的数 | 
Quick Sort
| 1 | def quicksort(nums, left, right): |