Presentation on theme: "Median Finding, Order Statistics & Quick Sort"— Presentation transcript:
1 Median Finding, Order Statistics & Quick Sort Vanithadevi Devarajan
2 Order Statistics 7th order statistic 3 4 13 14 23 27 41 54 65 75 The k-th order statistic is the k-th smallest element of an array.7th order statistic341314232741546575upper median341314232741lower median546575The lower median is the th order statisticThe upper median is the th order statisticIf n is odd, lower and upper median are the same
3 Given an unsorted array, how quickly one can find the i-th order statistics = A[i] ? By Sorting the array and retrieving the i-th value takes (n lg n),even though max (i=n) and min(i=1) can be done in (n).Can we do better in Linear Time?- Solved affirmatively in 1972 by (Manuel)Blum, Floyd, Pratt, Rivest, and Tarjan.- Described two Linear-Time algorithms- One Randomized and One Deterministic
4 Selection ( Deterministic and Randomized ) – Finding the Median in Linear TimeRandomized AlgorithmAfter the partitioning step, one can tell which subarray has theitem, one is looking for, by just looking at their sizes.So, only need to recursively examine one subarray, not two.For instance,1. If one is looking for the 87th-smallest element inan array of size 200, and after partitioning the “LESS”subarray (of elements less than the pivot), then justneed to find the 87th smallest element in LESS.2. On the other hand, if the “LESS” subarray has size 40, thenjust need to find the 87−40−1 = 46th smallest element inGREATER.3. And if the “LESS” subarray has size exactly 86 then, justreturn the pivot.(
5 Randomized/QuickSelect: Given an array A of size n and integer k ≤ n,Pick a pivot element p at random from A.Split A into subarrays - LESS and GREATER, by comparingeach element to p as in Quicksort. While we are at it, count the number of elements ( L ) going into LESS.3. (a) If L = k − 1, then output p.(b) If L > k − 1, output QuickSelect(LESS, k).(c) If L < k − 1, output QuickSelect(GREATER, k − L − 1)The expected number of comparisons for QuickSelect is O(n).
6 Deterministic Linear Time Algorithm Deterministic SelectGiven an array A of size n and integer k ≤ n,Group the array into n/5 groups of size 5 and find the medianof each group.2. Each group is then sorted and its median is selected.3. Recursively, find the median of the medians. Call this as ‘p’.Use p as a pivot to split the array into subarrays - LESS andGREATER.Recurse on the appropriate array to find the K-th smallestelement.DeterministicSelect makes O(n) comparisons to find the kth smallest in an array of size n.
7 Quick Sort Given array of some length n, Pick an element p of the array as the pivot (or halt if thearray has size 0 or 1).2. Split the array into sub-arrays LESS, EQUAL, and GREATERby comparing each element to the pivot. (LESS has allelements less than p, EQUAL has all elements equalto p, and GREATER has all elements greater than p).Recursively sort LESS and GREATER.Worst-case running time is O(n^2 )Randomized-QuicksortRun the Quicksort algorithm as given above, each timepicking a random element in the array as the pivot.Worst-case Expected-Time bound is O(n log n)
8 Project Implementations Implementation of QuickSort , where the pivot is chosen from the previous Order Statistics Algorithm.Implementation of Randomized QuickSort, that chooses a random element as the Pivot.Implementation of Real World Quick Sort with the following heuristics.If L and R partitions are of unequal sizes, sort the smallestpartition first.2. If array size <= 7, use insertion sort3. Use the following idea for getting the pivot.middle = (start+end)/2if array size > 40length = array size / 8pivot1 = median(start, start - length, start - (2*length))pivot2 = median(end, end+length, end + 2*length)pivot3 = median(middle,middle-length, middle+length)pivot = median(pivot1,pivot2,pivot3)elsepivot = median(start,middle,end)