Chapter 7: Sorting Algorithms

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
CSE 373: Data Structures and Algorithms
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.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively.
Chapter 7: Sorting Algorithms
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to 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.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Sorting Chapter 10.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
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).
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
Divide-And-Conquer Sorting Small instance.  n
Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
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.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 7 Sorting Spring 14
Advance Analysis of Algorithms
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
EE 312 Software Design and Implementation I
CSE 373 Data Structures and Algorithms
CSE 332: Sorting II Spring 2016.
Presentation transcript:

Chapter 7: Sorting Algorithms Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 7: Sorting Algorithms Quick Sort Lydia Sinapova, Simpson College

Quick Sort Basic Idea Code Analysis Advantages and Disadvantages Applications Comparison with Heap sort and Merge sort Animation

Basic Idea Pick one element in the array, which will be the pivot. Make one pass through the array, called a partition step, re-arranging the entries so that: entries smaller than the pivot are to the left of the pivot. entries larger than the pivot are to the right

Basic Idea Recursively apply quicksort to the part of the array that is to the left of the pivot, and to the part on its right. No merge step, at the end all the elements are in the proper order

Choosing the Pivot Some fixed element: e.g. the first, the last, the one in the middle. Bad choice - may turn to be the smallest or the largest element, then one of the partitions will be empty Randomly chosen (by random generator) - still a bad choice

Choosing the Pivot The median of the array (if the array has N numbers, the median is the [N/2] largest number). This is difficult to compute - increases the complexity.

Choosing the Pivot The median-of-three choice: take the first, the last and the middle element. Choose the median of these three elements.

Find the Pivot – Java Code int median3 ( int [ ]a, int left, int right) { int center = (left + right)/2; if (a [left] > a [ center]) swap (a [ left ], a [center]); if (a [center] > a [right]) swap (a[center], a[ right ]); swap(a [ center ], a [ right-1 ]); return a[ right-1 ]; }

Quick Sort – Java Code If ( left + 10 < = right) { // do quick sort } else insertionSort (a, left, right);

Quick Sort – Java Code { int i = left, j = right - 1; for ( ; ; ) { while (a [++ i ] < pivot) { } while ( pivot < a [- - j ] ) { } if (i < j) swap (a[ i ], a [ j ]); else break; } swap (a [ i ], a [ right-1 ]);   quickSort ( a, left, i-1);   quickSort (a, i+1, right); }

Implementation Notes while (pivot < a[--j]){ } Compare the two versions:  A. while (a[++i] < pivot) { } while (pivot < a[--j]){ } if ( i < j ) swap (a[i], a[j]); else break;  B. while (a[ i ] < pivot) { i++ ; } while (pivot < a[ j ] ) { j- - ; }   if ( i < j ) swap (a [ i ], a [ j ]);

Implementation Notes If we have an array of equal elements, the second code will never increment i or decrement j, and will do infinite swaps. i and j will never cross.

Complexity of Quick Sort Average-case O(NlogN) Worst Case: O(N2) This happens when the pivot is the smallest (or the largest) element. Then one of the partitions is empty, and we repeat recursively the procedure for N-1 elements.

Complexity of Quick Sort Best-case O(NlogN) The pivot is the median of the array, the left and the right parts have same size. There are logN partitions, and to obtain each partitions we do N comparisons (and not more than N/2 swaps). Hence the complexity is O(NlogN)

Analysis T(N) = T(i) + T(N - i -1) + cN The time to sort the file is equal to the time to sort the left partition with i elements, plus the time to sort the right partition with N-i-1 elements, plus the time to build the partitions.

Worst-Case Analysis T(N-2) = T(N-3) + c(N-2) T(N-3) = T(N-4) + c(N-3) The pivot is the smallest (or the largest) element  T(N) = T(N-1) + cN, N > 1 Telescoping:   T(N-1) = T(N-2) + c(N-1) T(N-2) = T(N-3) + c(N-2) T(N-3) = T(N-4) + c(N-3) …………...  T(2) = T(1) + c.2

Worst-Case Analysis T(N) + T(N-1) + T(N-2) + … + T(2) = = T(N-1) + T(N-2) + … + T(2) + T(1) + c(N) + c(N-1) + c(N-2) + … + c.2   T(N) = T(1) + c times (the sum of 2 thru N) = T(1) + c (N (N+1) / 2 -1) = O(N2)

Best-Case Analysis The pivot is in the middle T(N) = 2T(N/2) + cN  Divide by N:   T(N) / N = T(N/2) / (N/2) + c

Best-Case Analysis Telescoping: T(N) / N = T(N/2) / (N/2) + c …… T(2) / 2 = T(1) / (1) + c

Best-Case Analysis Add all equations: T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + …. + T(2) / 2 = = (N/2) / (N/2) + T(N/4) / (N/4) + … + T(1) / (1) + c.logN After crossing the equal terms: T(N)/N = T(1) + c*LogN T(N) = N + N*c*LogN = O(NlogN)

Advantages and Disadvantages One of the fastest algorithms on average Does not need additional memory (the sorting takes place in the array - this is called in-place processing ) Disadvantages: The worst-case complexity is O(N2)

Applications Commercial applications QuickSort generally runs fast No additional memory The above advantages compensate for the rare occasions when it runs with O(N2)

Applications Warning: unless you assume the worst-case response time Never use in applications which require a guarantee of response time: Life-critical (medical monitoring, life support in aircraft, space craft)    Mission-critical (industrial monitoring and control in handling dangerous materials, control for aircraft, defense, etc) unless you assume the worst-case response time

Comparison with Heap Sort O(NlogN) complexity quick sort runs faster, (does not support a heap tree) the speed of quick sort is not guaranteed

Comparison with Merge Sort Merge sort guarantees O(NlogN) time Merge sort requires additional memory with size N Usually Merge sort is not used for main memory sorting, only for external memory sorting