1 / 12

Median Finding, Order Statistics & Quick Sort

Median Finding, Order Statistics & Quick Sort. Vanithadevi Devarajan. 3. 4. 13. 14. 23. 27. 41. 54. 65. 75. 7th order statistic. The lower median is the - th order statistic The upper median is the - th order statistic

eavan
Télécharger la présentation

Median Finding, Order Statistics & Quick Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Median Finding, Order Statistics & Quick Sort Vanithadevi Devarajan

  2. 3 4 13 14 23 27 41 54 65 75 7th order statistic Thelower medianis the- th order statistic Theupper medianisthe - th order statistic If n is odd, lower and upper median are the same 3 4 13 14 23 27 41 54 65 75 lower median upper median Order Statistics Thek-th order statisticis the k-th smallest element of an array.

  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 Time Randomized Algorithm • After the partitioning step, one can tell which subarray has the • item, 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 in • an array of size 200, and after partitioning the “LESS” • subarray (of elements less than the pivot), then just • need to find the 87th smallest element in LESS. • 2. On the other hand, if the “LESS” subarray has size 40, then • just need to find the 87−40−1 = 46th smallest element in • GREATER. • 3. And if the “LESS” subarray has size exactly 86 then, just • return 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 comparing • each 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 Select • Given an array A of size n and integer k ≤ n, • Group the array into n/5 groups of size 5 and find the median • of 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 and • GREATER. • Recurse on the appropriate array to find the K-th smallest • element. • 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 the • array has size 0 or 1). • 2. Split the array into sub-arrays LESS, EQUAL, and GREATER • by comparing each element to the pivot. (LESS has all • elements less than p, EQUAL has all elements equal • to p, and GREATER has all elements greater than p). • Recursively sort LESS and GREATER. • Worst-case running time is O(n^2 ) • Randomized-Quicksort • Run the Quicksort algorithm as given above, each time • picking 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 smallest • partition first. • 2. If array size <= 7, use insertion sort • 3. Use the following idea for getting the pivot. • middle = (start+end)/2 • if array size > 40 • length = array size / 8 • pivot1 = 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) • else • pivot = median(start,middle,end)

  9. Performance Analysis

  10. Performance Analysis

  11. DEMO

  12. THANK YOU !

More Related