Topic 17 Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Garfield AP Computer Science
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Faster Sorting Methods Chapter 9 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Algorithms and Average Case Time Complexity
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
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.
CS 171: Introduction to Computer Science II Quicksort.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
CS 307 Fundamentals of Computer ScienceSorting and Searching 1 Topic 11 Sorting and Searching "There's nothing in your head the sorting hat can't see.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
Sorting21 Recursive sorting algorithms Oh no, not again!
Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.
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 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.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
1 Sorting and Searching "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
CS 221 – Computer Science II Sorting and Searching 1 "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you.
Sort Algorithms.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting: Advanced Techniques Smt Genap
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
Sorting – Part II CS 367 – Introduction to Data Structures.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CS 221 – Computer Science II Sorting and Searching 1 "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you.
Outline This topic covers merge sort
CSCI 104 Sorting Algorithms
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
CSE 143 Lecture 23: quick sort.
Sorting and Searching "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat,
SORTING AND SEARCHING.
CSC215 Lecture Algorithms.
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
CSE 373 Data Structures and Algorithms
Merge Sort (11.1) CSE 2011 Winter April 2019.
Workshop for CS-AP Teachers
Presentation transcript:

Topic 17 Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems." - Don Knuth

Previous Sorts  Insertion Sort and Selection Sort are both average case O(N 2 )  Today we will look at two faster sorting algorithms. –quicksort –mergesort CS314Fast Sorting 2

CS314Fast Sorting 3 Stable Sorting  A property of sorts  If a sort guarantees the relative order of equal items stays the same then it is a stable sort  [7 1, 6, 7 2, 5, 1, 2, 7 3, -5] –subscripts added for clarity  [-5, 1, 2, 5, 6, 7 1, 7 2, 7 3 ] –result of stable sort  Real world example: –sort a table in Wikipedia by one criteria, then anotherWikipedia –sort by country, then by major wins

CS314Fast Sorting 4 Quicksort  Invented by C.A.R. (Tony) Hoare  A divide and conquer approach that uses recursion 1.If the list has 0 or 1 elements it is sorted 2.otherwise, pick any element p in the list. This is called the pivot value 3. Partition the list minus the pivot into two sub lists according to values less than or greater than the pivot. (equal values go to either) 4.return the quicksort of the first list followed by the quicksort of the second list

CS314Fast Sorting 5 Quicksort in Action Pick middle element as pivot: 46 Partition list quick sort the less than list Pick middle element as pivot: quicksort the less than list, pivot now 5 {} quicksort the less than list, base case quicksort the greater than list Pick middle element as pivot: 17 and so on….

CS314Fast Sorting 6 Quicksort on Another Data Set Big O of Quicksort?

CS314Fast Sorting 7 private static void swapReferences( Object[] a, int index1, int index2 ) { Object tmp = a[index1]; a[index1] = a[index2]; a[index2] = tmp; } private void quicksort( Comparable[] list, int start, int stop ) { if(start >= stop) return; //base case list of 0 or 1 elements int pivotIndex = (start + stop) / 2; // Place pivot at start position swapReferences(list, pivotIndex, start); Comparable pivot = list[start]; // Begin partitioning int i, j = start; // from first to j are elements less than or equal to pivot // from j to i are elements greater than pivot // elements beyond i have not been checked yet for(i = start + 1; i <= stop; i++ ) { //is current element less than or equal to pivot if(list[i].compareTo(pivot) <= 0) { // if so move it to the less than or equal portion j++; swapReferences(list, i, j); } //restore pivot to correct spot swapReferences(list, start, j); quicksort( list, start, j - 1 ); // Sort small elements quicksort( list, j + 1, stop ); // Sort large elements }

Clicker Question 1  What are the best case and worst case Orders (Big O) for quicksort? BestWorst A.O(NlogN)O(N 2 ) B.O(N 2 )O(N 2 ) C.O(N 2 )O(N!) D.O(NlogN)O(NlogN) E.O(N)O(NlogN) CS314Fast Sorting 8

Clicker Question 2  Is quicksort always stable? A. Yes B. No CS314Fast Sorting 9

CS314Fast Sorting 10 Merge Sort Algorithm 1.If a list has 1 element or 0 elements it is sorted 2.If a list has more than 1 split into into 2 separate lists 3.Perform this algorithm on each of those smaller lists 4.Take the 2 sorted lists and merge them together Don Knuth cites John von Neumann as the creator of this algorithm

CS314Fast Sorting 11 Merge Sort When implementing one temporary array is used instead of multiple temporary arrays. Why?

CS314Fast Sorting 12 Merge Sort code /** * perform a merge sort on the data in c c c != null, all elements of c * are the same data type */ public static void mergeSort(Comparable[] c) { Comparable[] temp = new Comparable[ c.length ]; sort(c, temp, 0, c.length - 1); } private static void sort(Comparable[] list, Comparable[] temp, int low, int high) { if( low < high) { int center = (low + high) / 2; sort(list, temp, low, center); sort(list, temp, center + 1, high); merge(list, temp, low, center + 1, high); }

CS314Fast Sorting 13 Merge Sort Code private static void merge( Comparable[] list, Comparable[] temp, int leftPos, int rightPos, int rightEnd) { int leftEnd = rightPos - 1; int tempPos = leftPos; int numElements = rightEnd - leftPos + 1; //main loop while( leftPos <= leftEnd && rightPos <= rightEnd){ if( list[ leftPos ].compareTo(list[rightPos]) <= 0) { temp[ tempPos ] = list[ leftPos ]; leftPos++; } else{ temp[ tempPos ] = list[ rightPos ]; rightPos++; } tempPos++; } //copy rest of left half while( leftPos <= leftEnd){ temp[ tempPos ] = list[ leftPos ]; tempPos++; leftPos++; } //copy rest of right half while( rightPos <= rightEnd){ temp[ tempPos ] = list[ rightPos ]; tempPos++; rightPos++; } //Copy temp back into list for(int i = 0; i < numElements; i++, rightEnd--) list[ rightEnd ] = temp[ rightEnd ]; }

Clicker Question 3  What are the best case and worst case Orders (Big O) for mergesort? BestWorst A.O(NlogN)O(N 2 ) B.O(N 2 )O(N 2 ) C.O(N 2 )O(N!) D.O(NlogN)O(NlogN) E.O(N)O(NlogN) CS314Fast Sorting 14

Clicker Question 4  Is mergesort always stable? A. Yes B. No CS314Fast Sorting 15

Clicker Question 5  You have 1,000,000 items that you will be searching. How many searches need to be performed before the data is changed to make it worthwhile to sort the data before searching? A.5 B.40 C.1,000 D.10,000 E.500,000 CS314Fast Sorting 16

CS314Fast Sorting 17 Comparison of Various Sorts (2001) Num ItemsSelectionInsertionQuicksort expected? expected? expected? expected? expected? 1550 times in milliseconds, 1000 milliseconds = 1 second

CS314Fast Sorting 18 Comparison of Various Sorts (2001) Num ItemsSelectionInsertionQuicksort ?? ?? expected? expected? expected? expected? expected? times in seconds

Comparison of Various Sorts (2011) Num ItemsSelectionInsertionQuicksortMergeArrays.sort ???

Fast Sorting 20 Concluding Thoughts  Language libraries often have sorting algorithms in them –Java Arrays and Collections classes –C++ Standard Template Library –Python sort and sorted functions  Hybrid sorts –when size of unsorted list or portion of array is small use insertion sort, otherwise use O(N log N) sort like Quicksort or Mergesort

Concluding Thoughts  Sorts still being created!  Timsort (2002) –created for python version 2.3 –now used in Java version 7.0 –takes advantage of real world data –real world data is usually partially sorted, not totally random  Library Sort (2006) –Like insertion sort, but leaves gaps for later elements Fast Sorting 21