Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
In statistics, the kth order statistic of a statistical sample is equal to its kth-smallest value.[1]
(Simplified) real world quicksort with the following :
i. if L and R partitions are of unequal sizes, sort the smallest partition first.
ii. if array size <= 10, use insertion sort
//Input array A with size n and k ≤ n.
DeterministicSelect(A[0…n-1],K)
1)Divide array A to n/5 groups (each with size 5)
2)Look for the median in each group.
3) calculate the median of medians recursively. Say this is (p).
5)Select p to be the pivot and split A into 2 sub-arrays - sub-arrayLess and sub-arrayGreater.
6)find the K-th smallest element recursively from the suitable sub-array.
Sorted Array
iii.use median as pivot
p
Take the first, the last and the middle element.
Choose the median of these three elements.
Example:
8, 3, 25, 6, 10, 17, 1, 2, 18, 5
The first element is 8, the middle - 10, the last - 5.
The median of [8, 10, 5] is 8
Each time select any random element
in the array as the pivot.
Worst-case is O(nlogn)
//Input array A with size n and k - n.
QuickSelect(A[0…n-1],K)
1)Select a random pivot piv from A.
2)Divide A into two sub-arrays, one that contains elements LESS than the piv (sub-arrayLess) and the other one contains elements GREATER than the piv (sub-arrayGreater)
3)Count number of elements less than the piv (nLess)
If nLess = k - 1, output piv.
else If nLess > k -1,
call QuickSelect(sub-arrayLess, k).
else If nLess < k -1,
call QuickSelect(sub-arrayGreater, k - L - 1)
This is a bad choice - the pivot may turn to be the smallest or the largest element,
then one of the partitions will be empty.
P
Empty
P
if the array has N numbers, the median is the [N/2] largest number.
This is difficult to compute - increases the complexity.
Comparison between Algorithms
YES
O(N)
O(N)
n
n
n
n
key comparision
key comparision
key comparision
key comparision
O(NlogN)
Worst case O(N2)
NO
O(N)
Worst case o(n2)
YES