§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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]
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.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Quicksort.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Divide and Conquer Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Sorting and Lower bounds Fundamental Data Structures and Algorithms Ananda Guna January 27, 2005.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
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.
Analysis of Algorithms CS 477/677
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Instructor Neelima Gupta Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Sorting: Implementation Fundamental Data Structures and Algorithms Margaret Reid-Miller 24 February 2004.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Chapter 5 Sorting There are several easy algorithms to sort in O(N 2 ), such as insertion sort. There is an algorithm, Shellsort, that is very simple to.
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.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
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.
Quick Sort (11.2) CSE 2011 Winter November 2018.
EE 312 Software Design and Implementation I
Presentation transcript:

§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot = pick any element in A[ ]; Partition S = { A[ ] \ pivot } into two disjoint sets: A1={ a  S | a  pivot } and A2={ a  S | a  pivot }; A = Quicksort ( A1, N1)  { pivot }  Quicksort ( A2, N2); }   The best case T(N) = O( )N log N The pivot is placed at the right place once and for all. 1/15

§7 Quicksort 2. Picking the Pivot  A Wrong Way: Pivot = A[ 0 ] The worst case:A[ ] is presorted – quicksort will take O( N 2 ) time to do nothing   A Safe Maneuver: Pivot = random select from A[ ]  random number generation is expensive  Median-of-Three Partitioning: Pivot = median ( left, center, right ) Eliminates the bad case for sorted input and actually reduces the running time by about 5%. 2/15

§7 Quicksort 3. Partitioning Strategy i j >> j << i i < i > j < i < i < > j i < Not too difficult if we carefully implement it… What if there is a key == pivot? How about stop i and j both and then swap? What will happen to the sequence: 1, 1, 1, …, 1 ? Uh-oh, there will be many dummy swaps… But hey! At least the sequence will be partitioned into two equal-sized subsequences. Good point! How about the other option – that neither i nor j stops? No swap… but then T( N ) = … Then T( N ) = O( N 2 ). So we’d better stop both i and j and take some extra swaps. 3/15

§7 Quicksort 4. Small Arrays Problem: Quicksort is slower than insertion sort for small N (  20 ). Solution: Cutoff when N gets small ( e.g. N = 10 ) and use other efficient algorithms (such as insertion sort). 5. Implementation void Quicksort( ElementType A[ ], int N ) { Qsort( A, 0, N - 1 ); /* A: the array */ /* 0: Left index */ /* N – 1: Right index*/ } 4/15

§7 Quicksort /* Return median of Left, Center, and Right */ /* Order these and hide the pivot */ ElementType Median3( ElementType A[ ], int Left, int Right ) { int Center = ( Left + Right ) / 2; if ( A[ Left ] > A[ Center ] ) Swap( &A[ Left ], &A[ Center ] ); if ( A[ Left ] > A[ Right ] ) Swap( &A[ Left ], &A[ Right ] ); if ( A[ Center ] > A[ Right ] ) Swap( &A[ Center ], &A[ Right ] ); /* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */ Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ /* only need to sort A[ Left + 1 ] … A[ Right – 2 ] */ return A[ Right - 1 ]; /* Return pivot */ } 5/15

§7 Quicksort void Qsort( ElementType A[ ], int Left, int Right ) { int i, j; ElementType Pivot; if ( Left + Cutoff <= Right ) { /* if the sequence is not too short */ Pivot = Median3( A, Left, Right ); /* select pivot */ i = Left; j = Right – 1; /* why not set Left+1 and Right-2? */ for( ; ; ) { while ( A[ + +i ] < Pivot ) { } /* scan from left */ while ( A[ – –j ] > Pivot ) { } /* scan from right */ if ( i < j ) Swap( &A[ i ], &A[ j ] ); /* adjust partition */ else break; /* partition done */ } Swap( &A[ i ], &A[ Right - 1 ] ); /* restore pivot */ Qsort( A, Left, i - 1 ); /* recursively sort left part */ Qsort( A, i + 1, Right ); /* recursively sort right part */ } /* end if - the sequence is long */ else /* do an insertion sort on the short subarray */ InsertionSort( A + Left, Right - Left + 1 ); } 6/15

§7 Quicksort 6. Analysis T( N ) = T( i ) + T( N – i – 1 ) + c N  The Worst Case: T( N ) = T( N – 1 ) + c NT( N ) = O( N 2 )  The Best Case: [ ]  [ ] T( N ) = 2T( N / 2 ) + c NT( N ) = O( N log N )  The Average Case: Assume the average value of T( i ) for any i is T( N ) = O( N log N ) Home work: p More analysis on Quicksort 〖 Example 〗 Given a list of N elements and an integer k. Find the kth largest element. Read Figure 6.16 on p.214 for the 5 th algorithm on solving this problem. 7/15

§8 Sorting Large Structures Problem: Swapping large structures can be very much expensive. Solution: Add a pointer field to the structure and swap pointers instead – indirect sorting. Physically rearrange the structures at last if it is really necessary. list key table [0] d 0 [1] b 1 [2] f 2 [3] c 3 [4] a 4 [5] e 5 table The sorted list is list [ table[0] ], list [ table[1] ], ……, list [ table[n  1] ] Note: Every permutation is made up of disjoint cycles. list key table [0] d 4 [1] b 1 [2] f 3 [3] c 0 [4] a 5 [5] e 2 temp = d current = 0 next = 4 a e f c 2 3 d 3 In the worst case there are ? cycles and requires ? record moves.  N / 2  3N / 2  T = O( m N ) where m is the size of a structure. 〖 Example 〗 Table Sort 8/15

Laboratory Project 2 Sort Poems Detailed requirements can be downloaded from Don’t forget to sign you names and duties at the end of your report. Due: Friday, October 23rd, 2009 at 10:00pm 9/15

§9 A General Lower Bound for Sorting 【 Theorem 】 Any algorithm that sorts by comparisons only must have a worst case computing time of  ( N log N ). Proof: K 0  K 1 K 1  K 2 K 0  K 2 stop [0,1,2] stop [0,2,1] stop [2,0,1] TF TF K 0  K 2 K 1  K 2 stop [1,0,2] stop [1,2,0] stop [2,1,0] TF TF TF Decision tree for insertion sort on R 0, R 1, and R 2 When sorting N distinct elements, there are N! different possible results. Thus any decision tree must have at least N! leaves. If the height of the tree is k, then N!  2 k  1 (# of leaves in a complete binary tree)  k  log(N!) + 1 Since N!  (N/2) N/2 and log 2 N!  (N/2)log 2 (N/2) =  ( N log 2 N ) Therefore T(N) = k  c  N log 2 N. 10/15

§10 Bucket Sort and Radix Sort  Bucket Sort 〖 Example 〗 Suppose that we have N students, each has a grade record in the range 0 to 100 (thus there are M = 101 possible distinct grades). How to sort them according to their grades in linear time? count Algorithm { initialize count[ ]; while (read in a student’s record) insert to list count[stdnt.grade]; for (i=0; i<M; i++) { if (count[i]) output list count[i]; } T(N, M) = O( M+N ) What if M >> N ? 11/15

〖 Example 〗 Given N = 10 integers in the range 0 to 999 ( M = 1000 ) Is it possible to sort them in linear time?  Radix Sort Input: 64, 8, 216, 512, 27, 729, 0, 1, 343, Bucket Sort according to the Least Significant Digit first. 0 Pass Pass Pass Output: 0, 1, 8, 27, 64, 125, 216, 343, 512, 729 T=O(P(N+B)) where P is the number of passes, N is the number of elements to sort, and B is the number of buckets. What if we sort according to the Most Significant Digit first? §10 Bucket Sort and Radix Sort 12/15

 K i j ::= the j-th key of record R i  K i 0 ::= the most significant key of record R i Suppose that the record R i has r keys.  K i r  1 ::= the least significant key of record R i  A list of records R 0,..., R n  1 is lexically sorted with respect to the keys K 0, K 1,..., K r  1 iff That is, K i 0 = K i+1 0,..., K i l = K i+1 l, K i l+1 < K i+1 l+1 for some l < r  1. 〖 Example 〗 A deck of cards sorted on 2 keys K 0 [Suit]  <  < <  K 1 [Face value]2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A Sorting result : 2 ... A  2 ... A  2... A 2 ... A  §10 Bucket Sort and Radix Sort 13/15

 MSD ( Most Significant Digit ) Sort  Sort on K 0 : for example, create 4 buckets for the suits 33 33   55 55    AA AA  4 4  Sort each bucket independently (using any sorting technique)  §10 Bucket Sort and Radix Sort 14/15

§10 Bucket Sort and Radix Sort  LSD ( Least Significant Digit ) Sort  Sort on K 1 : for example, create 13 buckets for the face values 22 22   33 33   4 4 55 55    AA AA ...  Reform them into a single pile AA AA  33 33   22 22    Create 4 buckets and resort Question: Is LSD always faster than MSD? Home work: p An application of Bucket Sort [ Think: why is it faster than O(N log N)? ] 15/15