Median Finding, Order Statistics & Quick Sort

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

1 More Sorting; Searching Dan Barrish-Flood. 2 Bucket Sort Put keys into n buckets, then sort each bucket, then concatenate. If keys are uniformly distributed.
CSE 3101: Introduction to the Design and Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Medians and Order Statistics
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Spring 2015 Lecture 5: QuickSort & Selection
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
Analysis of Quicksort. Quicksort Algorithm Given an array of n elements (e.g., integers): If array only contains one element, return Else –pick one element.
Median/Order Statistics Algorithms
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
Sorting Algorithms and Average Case Time Complexity
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Sorting Chapter 9.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Order Statistics(Selection Problem)
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
1 Medians and Order Statistics CLRS Chapter 9. upper median lower median The lower median is the -th order statistic The upper median.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
QuickSort Choosing a Good Pivot Design and Analysis of Algorithms I.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Dr. Yingwu Zhu Chapter 9, p Linear Time Selection Dr. Yingwu Zhu Chapter 9, p
CS 3343: Analysis of Algorithms
Chapter 9: Medians and Order Statistics
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
CS 583 Analysis of Algorithms
The Selection Problem.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Medians and Order Statistics
Presentation transcript:

Median Finding, Order Statistics & Quick Sort Vanithadevi Devarajan

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 statistic 3 4 13 14 23 27 41 54 65 75 upper median 3 4 13 14 23 27 41 lower median 54 65 75 The lower median is the - th order statistic The upper median is the - th order statistic If n is odd, lower and upper median are the same

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

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. (

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).

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.

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)

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)

Performance Analysis Array Size Quick Sort (Linear Median) (milliseconds) (Random Median) Randomized Quick Sort RealWorld QuickSort 100 1000 5000 15 10000 16 32 50000 63 31 78 75000 110 47 62 100000 156 79

Performance Analysis

DEMO

THANK YOU !