CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.

Slides:



Advertisements
Similar presentations
Bucket-Sort and Radix-Sort B 1, c7, d7, g3, b3, a7, e 
Advertisements

Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CSC 213 – Large Scale Programming. Today’s Goals  Review discussion of merge sort and quick sort  How do they work & why divide-and-conquer?  Are they.
Goodrich, Tamassia Sorting, Sets and Selection1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CSCE 3110 Data Structures & Algorithm Analysis
Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
Quicksort Quicksort     29  9.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
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.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
CSC 213 – Large Scale Programming Lecture 24: Radix & Bucket Sorts.
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.
© 2004 Goodrich, Tamassia Merge Sort1 Chapter 8 Sorting Topics Basics Merge sort ( 合併排序 ) Quick sort Bucket sort ( 分籃排序 ) Radix sort ( 基數排序 ) Summary.
CSC 213 – Large Scale Programming. Today’s Goals  Review past discussion of data sorting algorithms  Weaknesses of past approaches & when we use them.
Sorting.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2004 Goodrich, Tamassia Bucket-Sort and Radix-Sort B 1, c7, d7, g3, b3, a7, e 
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
© 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.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
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 7 2  9 4   2   4   7 2  2 9  9 4  4.
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,
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
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Bucket-Sort and Radix-Sort
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Bucket-Sort and Radix-Sort
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
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
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CS203 Lecture 15.
Bucket-Sort and Radix-Sort
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort

Quick-Sort (§ 10.2) Quick-sort is another divide-and-conquer sorting algorithm: Divide: pick an element x (the pivot) and split into 2 partitions:  L -- elements less than x  G -- elements equal to or bigger than x Recur : sort L and G Conquer: merge L, x and G x x L G x

Divide Partitioning is slightly more comple than merge-sort : In turn, remove each element y from S Compare y and pivot. Insert y into L or G depending on comparison result Only insert and remove first/last elements, so each insertion and removal takes O(1) time Partitioning takes _______ time Algorithm partition(S, C, pivot) while  S.isEmpty() y  S.removeFirst() comp = C.compare(y, pivot) if comp < 0 L.insertLast(y) else // e.g., comp > 0 G.insertLast(y) return L, pivot, G

Execution Tree Can depict execution using binary tree Node still represent recursive quick-sort calls  Left side shows initial sequence and highlights pivot  Right side shows final (sorted) sequence Root node is the initial call Leaves show base cases -- calls of size 0 or     29  9

 Execution Example  First, select a pivot

Execution Example Partition, recurse and select that pivot   

Execution Example Partition, recurse and enjoy the base case   11  

Execution Example Recursive call to sort the G partition   11  14 3   94  44    3 4

Execution Example Merge: Add entries from L, pivot, & then G   11  14 3   94  44  

Execution Example    11  14 3   94  44  4 Recursively solve G partition  

Execution Example Recursive calls and handle the base case     11  14 3   94  44     99  99  9

Execution Example 8    11  14 3   94  44    Merge: Add entries from L, pivot, & then G 7 9   99  99  9

Execution Example Merge: Add entries from L then E then G 8    11  14 3   94  44      99  99  9

Worst-case Running Time Worst possible case for quick-sort : we keep pivoting around the minimum or maximum elements Either L or G will have size n  1 and the other has size 0 Big-Oh Running time is therefore equal to the sum n  (n  1)  …  2  Total running time is therefore ____________ depthtime 0n 1 n  1 …… 1 …

Quick-Sort Pivot In theory, any Entry can be the pivot Algorithm only requires there be one In reality, want median Entry as pivot Traditionally used first or last Entry This means data already sorted or in reverse order is the worst case  Also fairly common cases. Oops, our Bad. Now usually select an Entry at random

Expected Running Time Consider recursive call of quick-sort on a sequence of size n Good call: both L and G have fewer than 3n / 4 entries Bad call: either L or G has more than 3n / 4 entries ½ of the possible pivots result in a good call  Good callBad call Good pivotsBad pivots

Expected Running Time Toss coin 2 n times to get n heads on average For node at depth i, we can expect i / 2 ancestors were good calls Input sequence would be at most n *(¾) i /2 Expect nodes at depth 2* log 4/3 n to be leaves Expected tree height = O (2*log n ) = O (log n ) Process nearly all of the original entries at each depth Expected running time of quick-sort is ______

We Can Rebuild Her… Of previous sorts, only Bubble-sort does not use additional data structures Selection-sort & Insertion-sort can use a Sequence-based priority queue Heap-sort can use a heap-based priority queue (though this can be avoided) Merge-sort uses additional Sequences We can modify Quick-sort to run in- place

In-Place Quick-Sort During partitioning, rearrange entries in the sequence such that: entries smaller than pivot (e.g., L ) have rank less than l pivot has rank between l entries larger than pivot (e.g., G ) have rank greater than l First partition S into L and G and then move pivot in between these partitions

In-Place Partitioning jk Algorithm inPlacePartition(Sequence S, int j, int k, Entry pivot) while j < k do while C.compare(S.elemAtRank(j), pivot) < 0 do j  j + 1 while j < k and C.compare(S.elemAtRank(k), pivot) ≥ 0 do k  k – 1 if (j < k) then S.swapElement(j, k) // j contains the rank of the first element in G j k j k

In-Place Quick-Sort Algorithm inPlaceQuickSort(Sequence S, int lower, int upper) if lower < upper then i  select a new pivot rank between lower and upper pivot  S.elemAtRank(i) remove pivot from S h  inPlacePartition(S, lower, upper-1, pivot) inPlaceQuickSort(S, lower, h - 1) inPlaceQuickSort(S, h, upper) S.insertAtRank(h, pivot) else // If lower == upper, we only have 1 Entry to sort // If lower > upper, then no Entries to sort. // So these are our base cases; we do not have to do anything! endif

Summary of Sorting Algorithms AlgorithmTimeNotes selection-sort O(n2)O(n2) uses priority queue slow (good for small inputs) insertion-sort O(n2)O(n2) uses priority queue slow (good for small inputs) quick-sort expected O(n log n) in-place, randomized fastest (good for large inputs) heap-sort O(n log n) uses additional heap fast (good for large inputs) merge-sort O(n log n) sequential data access fast (good for huge inputs)

Can We Do Better? Comparing entries to another we cannot do better than O ( n log n ) time We will talk more about this in a week But what if we had a sort that did not do not actually compare the entries Any ideas (from non-BIF400 students) how this would work?

Bucket-Sort (§ ) Bucket-sort maintains auxiliary array, B, of Sequences (e.g., buckets) Bucket-sort sorts a Sequence, S, based upon the entry’s key: Phase 1: Move each entry in S to the end of bucket B[k], where k is the entry’s key Phase 2: For i  0, …, B.size ()-1, move entries in bucket B[i] to end of sequence S

Bucket-Sort Example Suppose keys range from [0, 9] 7, d1, c3, a7, g3, b7, e 1, c3, a3, b7, d7, g7, e Phase 1 Phase B 1, c7, d7, g3, b3, a7, e  S 7   S

Bucket-Sort Algorithm Algorithm bucketSort(Sequence S, Comparator c) B  new Sequence[c.getMaxKey()] // instantiate the Sequence at each index within B while  S.isEmpty() do // Phase 1 entry  S.removeFirst() B[c.compare(entry, null)].insertLast(entry) for i  0 to B.length - 1 // Phase 2 while  B[i].isEmpty() do entry  B[i].removeFirst() S.insertLast(entry) return S

Bucket-Sort Properties Keys are indices into an array Must be non-negative integers and not arbitrary objects Does not need a Comparator Stable Sort Preserves relative ordering of two entries with same key Bubble-sort and Merge-sort can also be stable

Bucket-Sort Extensions Extend Bucket-sort with Comparator Specifies maximum number of buckets compare ( key, null ) returns index to which a key is mapped For Integer keys in range a – b: Comparator maps key k to k – a For Boolean keys, Comparator returns: index of 0 if key is false index of 1 if key is true

Bucket-Sort Extensions Can actually use Bucket-sort with any keys who can must be from some bounded set, D, of values E.g., D could be U.S. states, molecular structures, people I want to kill, … Comparator must have rank for each value in D  E.g., Rank states in alphabetical order or order of admission  Ranks then serve as the indicies

d -Tuples d- tuple is combination of d keys (k 1, k 2, …, k d ) k i is the “ i- th dimension of the tuple” Example: Points on a plane are a 2-tuple X-axis is 1 st dimension, Y-axis is 2 nd dimension

Lexicographic Order Lexicographic order of d- tuples is defined recursively as: (x 1, x 2, …, x d )  (y 1, y 2, …, y d )  x 1  y 1  ( x 1   y 1  (x 2, …, x d )  (y 2, …, y d )) How would we order following tuples? (3, 4) (7, 8) (3, 2) (1, 4) (4, 8) (1, 4) (3, 2) (3, 4) (4, 8) (7, 8)

Lexicographic Sorting Arranges d -tuples by making d calls to a stable sorting algorithm E.g., One call to sort each tuple dimension Concept lexicographicSort(Sequence s, Comparator c, Sort stableSort) for i  c.size() downto 1 stableSort.sort(s, c, i) return s

Lexicographic Sorting Example Example: (7,4,6) (5,1,5) (2,4,6) (2, 1, 4) (3, 2, 4)

Lexicographic Sorting Example Example: (7,4,6) (5,1,5) (2,4,6) (2,1,4) (3,2,4) (2,1,4) (3,2,4) (5,1,5) (7,4,6) (2,4,6) (2,1,4) (5,1,5) (3,2,4) (7,4,6) (2,4,6) (2,1,4) (2,4,6) (3,2,4) (5,1,5) (7,4,6)

Radix-Sort (§ ) Radix-sort is a lexicographical sort that uses Bucket-sort as its stable sorting algorithm Radix-sort is applicable to tuples where the keys in each dimension i can be made into indexes in the range [0, N -1] Note: Integers are keys that can be considered to have multiple dimensions Pass i as compare ’s second parameter

Radix-Sort for Integers Can represent integer as a b -tuple of digits or bits: 62 = = Decimal representation uses need 10 buckets Binary numbers need 2 buckets Radix-sort algorithm runs in O(bn) time For 32-bit integers b = 32 Radix-sort sort Sequences using integer keys in O(32n) time

Radix-Sort for Integers Algorithm binaryRadixSort(Sequence S, Comparator c) for i  0 to 31 bucketSort(S, 2, i, c) return S If key is an int, the value of the i th bit of an integer is: (( key >> i ) & 1)

Example Sorting a sequence of 4-bit integers

Daily Quiz Always selecting the first entry as the pivot, show the execution tree for Quick-sort when sorting the following in the order given Sequence with keys: Sequence with keys: Sequence with keys: