首页 > 代码库 > Kth order statistcs
Kth order statistcs
Selection:
selection is a trivial problem if the input numbers are sorted. If we use a sorting algorithm having O(nlgn) worst case running time, then the selection problem can be solved in O(nlgn) time. But using a sorting is more like using a cannon to shoot a fly since only one number needs to computed.
O(n) expected-time selection using the randomized partition.
Idea: In order to find the k-th order statistics in a region of size n, use the randomized partition to split the region into two subarrays. Let s-1 and n-s be the size of the left subarray and the size of the right subarray. If k=s, the pivot is the key that‘s looked for. If k<= s-1, look for the k-th element in the left subarray. Otherwise, look for the (k-s)-th one in the right subarray.