Quick-Sort 2/18/2018 3:56 AM Selection Selection.

Slides:



Advertisements
Similar presentations
Goodrich, Tamassia Sorting, Sets and Selection1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Advertisements

Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Introduction to Algorithms
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
Quicksort Quicksort     29  9.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 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.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
1 CSC401 – Analysis of Algorithms Lecture Notes 9 Radix Sort and Selection Objectives  Introduce no-comparison-based sorting algorithms: Bucket-sort and.
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.
CSC 213 Lecture 15: Sets, Union/Find, and the Selection Problem.
Sorting.
CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.
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.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
LECTURE 40: SELECTION CSC 212 – Data Structures.  Sequence of Comparable elements available  Only care implementation has O(1) access time  Elements.
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Quick-Sort To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select.
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Median Finding and Quick Sort
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
Divide-and-Conquer 7 2  9 4   2   4   7
Medians and Order Statistics
1 Lecture 13 CS2013.
Sorting Recap & More about Sorting
Sorting, Sets and Selection
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

Quick-Sort 2/18/2018 3:56 AM Selection Selection

The Selection Problem Given an integer k and n elements x1, x2, …, xn, taken from a total order, find the k-th smallest element in this set. Of course, we can sort the set in O(n log n) time and then index the k-th element. Can we solve the selection problem faster? k=3 7 4 9 6 2  2 4 6 7 9 Selection

Quick-Select (§10.7) Quick-select is a randomized selection algorithm based on the prune-and-search paradigm: Prune: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Search: depending on k, either answer is in E, or we need to recur on either L or G x x L G E k > |L|+|E| k’ = k - |L| - |E| k < |L| |L| < k < |L|+|E| (done) Selection

Partition We partition an input sequence as in the quick-sort algorithm: We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick-select takes O(n) time Algorithm partition(S, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G  empty sequences x  S.remove(p) while S.isEmpty() y  S.remove(S.first()) if y < x L.insertLast(y) else if y = x E.insertLast(y) else { y > x } G.insertLast(y) return L, E, G Selection

Quick-Select Visualization An execution of quick-select can be visualized by a recursion path Each node represents a recursive call of quick-select, and stores k and the remaining sequence k=5, S=(7 4 9 3 2 6 5 1 8) k=2, S=(7 4 9 6 5 8) k=2, S=(7 4 6 5) k=1, S=(7 6 5) 5 Selection

Expected Running Time Consider a recursive call of quick-select on a sequence of size s Good call: the sizes of L and G are each less than 3s/4 Bad call: one of L and G has size greater than 3s/4 A call is good with probability 1/2 1/2 of the possible pivots cause good calls: 7 2 9 4 3 7 6 1 9 7 2 9 4 3 7 6 1 2 4 3 1 7 9 7 1  1 1 7 2 9 4 3 7 6 Good call Bad call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bad pivots Good pivots Bad pivots Selection

Expected Running Time, Part 2 Probabilistic Fact #1: The expected number of coin tosses required in order to get one head is two Probabilistic Fact #2: Expectation is a linear function: E(X + Y ) = E(X ) + E(Y ) E(cX ) = cE(X ) Let T(n) denote the expected running time of quick-select. By Fact #2, T(n) < T(3n/4) + bn*(expected # of calls before a good call) By Fact #1, T(n) < T(3n/4) + 2bn That is, T(n) is a geometric series: T(n) < 2bn + 2b(3/4)n + 2b(3/4)2n + 2b(3/4)3n + … So T(n) is O(n). We can solve the selection problem in O(n) expected time. Selection

Deterministic Selection We can do selection in O(n) worst-case time. Main idea: recursively use the selection algorithm itself to find a good pivot for quick-select: Divide S into n/5 sets of 5 each Find a median in each set Recursively find the median of the “baby” medians. See Exercise C-4.24 for details of analysis. 1 2 3 4 5 Min size for L for G Selection