Quicksort and Mergesort

Slides:



Advertisements
Similar presentations
HST 952 Computing for Biomedical Scientists Lecture 9.
Advertisements

Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting Gordon College 13.1 Some O(n2) Sorting Schemes
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting21 Recursive sorting algorithms Oh no, not again!
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
Quicksort Dr. Yingwu Zhu. 2 Quicksort A more efficient exchange sorting scheme than bubble sort – A typical exchange involves elements that are far apart.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Analysis of Algorithms CS 477/677
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CSCI 104 Sorting Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Sorting Why? Displaying in order Faster Searching Categories Internal
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Data Structures Using C++ 2E
Chapter 7 Sorting Spring 14
Description Given a linear collection of items x1, x2, x3,….,xn
Algorithm Design Methods
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
Unit-2 Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Sub-Quadratic Sorting Algorithms
Chapter 4.
EE 312 Software Design and Implementation I
Algorithm Efficiency and Sorting
Final Review Dr. Yingwu Zhu.
Adapted from instructor resource slides
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Sorting Chapter 10.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Design and Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
CS203 Lecture 15.
Algorithm Efficiency and Sorting
Divide and Conquer Merge sort and quick sort Binary search
Advanced Sorting Methods: Shellsort
Presentation transcript:

Quicksort and Mergesort Dr. Yingwu Zhu Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort A more efficient exchange sorting scheme than bubble sort A typical exchange involves elements that are far apart Fewer interchanges are required to correctly position an element. Quicksort uses a divide-and-conquer strategy A recursive approach The original problem partitioned into simpler sub-problems, Each sub problem considered independently. Subdivision continues until sub problems obtained are simple enough to be solved directly How simple? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Choose some element called a pivot Perform a sequence of exchanges so that All elements that are less than this pivot are to its left. All elements that are greater than the pivot are to its right. Divides the (sub)list into two smaller sub lists, Each of which may then be sorted independently in the same way. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort If the list has 0 or 1 elements, return. // the list is sorted, simple enough! Else do: Pick an element in the list to use as the pivot.   Split the remaining elements into two disjoint groups: SmallerThanPivot = {all elements <= pivot} LargerThanPivot = {all elements > pivot}    Return the list rearranged as: Quicksort(SmallerThanPivot), pivot, Quicksort(LargerThanPivot). Split Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Example Given to sort: 75, 70, 65, , 98, 78, 100, 93, 55, 61, 81, Select, arbitrarily, the first element, 75, as pivot. Search from right for elements <= 75, stop at first element <=75 Search from left for elements > 75, stop at first element >75 Swap these two elements, and then repeat this process 84 68 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Example When done, swap with pivot 75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84 When done, swap with pivot This SPLIT operation placed pivot 75 so that all elements to the left were <= 75 and all elements to the right were > 75. View code for split() 75 is now placed appropriately Need to sort sublists on either side of 75 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Example Need to sort (independently): 55, 70, 65, 68, 61 and 100, 93, 78, 98, 81, 84 Let pivot be 55, look from each end for values larger/smaller than 55, swap Same for 2nd list, pivot is 100 Sort the resulting sublists in the same manner until sublist is trivial (size 0 or 1) View quicksort() recursive function Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Note visual example of a quicksort on an array etc. … Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

int split(T x[], int first, int last) { T pivot = x[first]; typedef int T; int split(T x[], int first, int last) { T pivot = x[first]; int left = first, right = last; while(left < right) { while(left < right && pivot < x[right]) right--; while(left < right && x[left] <= pivot) left++; if (left < right) swap(x[left], x[right]); } //end while x[first] = x[right]; x[right] = pivot; return right; } Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Reflection of Quicksort Perform spilt() operation on a (sub)list, such that: left-sublist, pivot, right-sublist Recursively and independently perform split() on left-sublist and right-sublist, until their sizes become 0 or 1 (simple enought). So, the basic operation is split! int split(int x[], int low, int high) [low, high] specifies the sublist. Returns the final position of the pivot Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Recursive Quicksort void quicksort(int x[], int n) Probably needs an additional function? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

void quicksort(int x[], int n) { quicksort_aux(x,0,n-1); } void quicksort_aux(int x[], int first, int last) { if (first < last) { int pos = split(x, first, last); quicksort_aux(x, first, pos-1); quicksort_aux(x, pos+1, last); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort: T(n) Best-case ? Worst-case ? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Quicksort Performance O(nlog2n) is the best case computing time If the pivot results in sublists of approximately the same size. O(n2) worst-case List already ordered, elements in reverse When Split() repetitively results, for example, in one empty sublist Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improvements to Quicksort Quicksort is a recursive function stack of activation records must be maintained by system to manage recursion. The deeper the recursion is, the larger this stack will become. The depth of the recursion and the corresponding overhead can be reduced sort the smaller sublist at each stage first Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improvements to Quicksort Another improvement aimed at reducing the overhead of recursion is to use an iterative version of Quicksort() To do so, use a stack to store the first and last positions of the sublists sorted "recursively". Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improvements to Quicksort An arbitrary pivot gives a poor partition for nearly sorted lists (or lists in reverse) Virtually all the elements go into either SmallerThanPivot or LargerThanPivot all through the recursive calls. Quicksort takes quadratic time to do essentially nothing at all. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improvements to Quicksort Better method for selecting the pivot is the median-of-three rule, Select the median of the first, middle, and last elements in each sublist as the pivot. Often the list to be sorted is already partially ordered Median-of-three rule will select a pivot closer to the middle of the sublist than will the “first-element” rule. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improvements to Quicksort For small files (n <= 20), quicksort is worse than insertion sort; small files occur often because of recursion. Use an efficient sort (e.g., insertion sort) for small files. Better yet, use Quicksort() until sublists are of a small size and then apply an efficient sort like insertion sort. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Non-recursive Quicksort Think about non-recursive alg.? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Mergesort Sorting schemes are either … internal -- designed for data items stored in main memory external -- designed for data items stored in secondary memory. Previous sorting schemes were all internal sorting algorithms: required direct access to list elements not possible for sequential files made many passes through the list not practical for files Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Mergesort Mergesort can be used both as an internal and an external sort. Basic operation in mergesort is merging, combining two lists that have previously been sorted resulting list is also sorted. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Merge Algorithm 1. Open File1 and File2 for input, File3 for output 2. Read first element x from File1 and first element y from File2 3. While neither eof File1 or eof File2 If x < y then a. Write x to File3 b. Read a new x value from File1 Otherwise a. Write y to File3 b. Read a new y from File2 End while 4. If eof File1 encountered copy rest of of File2 into File3. If eof File2 encountered, copy rest of File1 into File3 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Merge Algorithm void merge(int F[ ], int& n, int F1[ ], int n1, int F2[ ], int n2); //F1 and F2 are sorted, the resulting F is sorted Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Merge Sort Given a single file Split into two files Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Merge Sort Merge first one-element "subfile" of F1 with first one-element subfile of F2 Gives a sorted two-element subfile of F Continue with rest of one-element subfiles Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Merge Sort Split again Merge again as before Each time, the size of the sorted subgroups doubles Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Note we always are limited to subfiles of some power of 2 Binary Merge Sort Last splitting gives two files each in order Last merging yields a single file, entirely in order Note we always are limited to subfiles of some power of 2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort Allows sorted subfiles of other sizes Number of phases can be reduced when file contains longer "runs" of ordered elements Consider file to be sorted, note in order groups Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort Copy alternate groupings into two files Use the sub-groupings, not a power of 2 Look for possible larger groupings Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

EOF for F2, Copy remaining groups from F1 Natural Merge Sort Merge the corresponding sub files EOF for F2, Copy remaining groups from F1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort Split again, alternating groups Merge again, now two subgroups One more split, one more merge gives sort Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort void split(int F[ ], int n, int F1[ ], int&n1, int F2[ ], int& n2); int multi_merge(int F[ ], int& n, int F1[ ], int n1, int F2[ ], int n2 ); //return # of resulting sorted subfiles in F[ ] //key: keep track of sorted subfiles, how? void natural_merge(int F[ ], int n); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort T(n) = ? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Natural Merge Sort T(n) = O(n log2n), why? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Exercise One variation of the mergesort is to modify the splitting operation as follows: copy some fixed number of elements into main memory, sort them using an internal sorting method as quicksort, and write this sorted list to F1; then read the same number of elements from F into main memory, sort them internally, and write this sorted list to F2; and so on, alternating between F1 and F2. Write a function for this modified mergesort, using quicksort to sort internally the sublists containing SIZE elements for some constant SIZE. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3