CS221: Algorithms and Data Structures Lecture #4 Sorting Things Out Steve Wolfman 2014W1 1.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Garfield AP Computer Science
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Using Divide and Conquer for Sorting
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, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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. 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.
CS 171: Introduction to Computer Science II Quicksort.
CPSC 320: Intermediate Algorithm Design & Analysis Asymptotic Notation: (O, Ω, Θ, o, ω) Steve Wolfman 1.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
Divide and Conquer Sorting
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
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.
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.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Divide-And-Conquer Sorting Small instance.  n
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.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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.
1 Lecture 16: Lists and vectors Binary search, Sorting.
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.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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.
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2014W1 1.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
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.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
CS221: Algorithms and Data Structures Lecture #4 Sorting Things Out Steve Wolfman 2011W2 1.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
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.
Lecture No.45 Data Structures Dr. Sohail Aslam.
CS221: Algorithms and Data Structures Lecture #4 Sorting Things Out
Quicksort analysis Bubble sort
CSC215 Lecture Algorithms.
Data Structures Review Session
Sub-Quadratic Sorting Algorithms
CSE 326: Data Structures Sorting
CSE 373 Data Structures and Algorithms
Presentation transcript:

CS221: Algorithms and Data Structures Lecture #4 Sorting Things Out Steve Wolfman 2014W1 1

Quick Review of Sorts Insertion Sort: Keep a list of already sorted elements. One by one, insert new elements into the right place in the sorted list. Selection Sort: Repeatedly find the smallest (or largest) element and put it in the next slot (where it belongs in the final sorted list). Merge Sort: Divide the list in half, sort the halves, merge them back together. (Base case: length  1.) 2

Insertion Sort Invariant 3 in sorted orderuntouched each iteration moves the line right one element

Selection Sort Invariant 4 smallest, 2 nd smallest, 3 rd smallest, … in order remainder in no particular order each iteration moves the line right one element

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 5

Categorizing Sorting Algorithms Computational complexity –Average case behaviour: Why do we care? –Worst/best case behaviour: Why do we care? How often do we re-sort sorted, reverse sorted, or “almost” sorted (k swaps from sorted where k << n) lists? Stability: What happens to elements with identical keys? Memory Usage: How much extra memory is used? 6

Comparing our “PQSort” Algorithms Computational complexity –Selection Sort: Always makes n passes with a “triangular” shape. Best/worst/average case  (n 2 ) –Insertion Sort: Always makes n passes, but if we’re lucky and search for the maximum from the right, only constant work is needed on each pass. Best case  (n); worst/average case:  (n 2 ) –Heap Sort: Always makes n passes needing O(lg n) on each pass. Best/worst/average case:  (n lg n). 7 Note: best cases assume distinct elements. With identical elements, Heap Sort can get  (n) performance.

Insertion Sort Best Case PQ If we search from the right: constant time per pass!

Comparing our “PQSort” Algorithms Stability –Selection: Unstable. (Say we swap in the “leftmost” smallest element, what about the element swapped out?) –Insertion: Easily made stable (when building from the left, find the rightmost slot for a new element). –Heap: Unstable  Memory use: All three are essentially “in-place” algorithms with small O(1) extra space requirements. Cache access: Not detailed in 221, but… algorithms that don’t “jump around” tend to perform better in modern memory systems. Which of these “jumps around”? 9 But note: there’s a trick to make any sort stable.

Comparison of growth... nlgn n2n2 n n=100 T(n)= Reminder: asymptotic differences matter, but a factor of lg n doesn’t matter as much as a factor of n.

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 11

MergeSort Mergesort belongs to a class of algorithms known as “divide and conquer” algorithms (your recursion sense should be tingling here...). The problem space is continually split in half, recursively applying the algorithm to each half until the base case is reached. 12

MergeSort Algorithm 1.If the array has 0 or 1 elements, it’s sorted. Else… 2.Split the array into two halves 3.Sort each half recursively (i.e., using mergesort) 4.Merge the sorted halves to produce one sorted result: 1.Consider the two halves to be queues. 2.Repeatedly compare the fronts of the queues. Whichever is smaller (or, if one is empty, whichever is left), dequeue it and insert it into the result. 13

MergeSort Performance Analysis 1.If the array has 0 or 1 elements, it’s sorted. Else… 2.Split the array into two halves 3.Sort each half recursively (i.e., using mergesort) 4.Merge the sorted halves to produce one sorted result: 1.Consider the two halves to be queues. 2.Repeatedly compare the fronts of the queues. Whichever is smaller (or, if one is empty, whichever is left), dequeue it and insert it into the result. T(1) = 1 2*T(n/2) n 14

MergeSort Performance Analysis T(1) = 1 T(n) = 2T(n/2) + n = 4T(n/4) + 2(n/2) + n = 8T(n/8) + 4(n/4) + 2(n/2) + n = 8T(n/8) + n + n + n = 8T(n/8) + 3n = 2 i T(n/2 i ) + in. Let i = lg n T(n)= nT(1) + n lg n = n + n lg n   (n lg n) We ignored floors/ceilings. To prove performance formally, we’d use this as a guess and prove it with floors/ceilings by induction. 15

Try it on this array!

Try it on this array! * ** Where does the red 3 go?

Mergesort (by Jon Bentley): void msort(int x[], int lo, int hi, int tmp[]) { if (lo >= hi) return; int mid = (lo+hi)/2; msort(x, lo, mid, tmp); msort(x, mid+1, hi, tmp); merge(x, lo, mid, hi, tmp); } void mergesort(int x[], int n) { int *tmp = new int[n]; msort(x, 0, n-1, tmp); delete[] tmp; } 18

Merge (by Jon Bentley): void merge(int x[],int lo,int mid,int hi, int tmp[]) { int a = lo, b = mid+1; for( int k = lo; k <= hi; k++ ) { if( a hi || x[a] < x[b]) ) tmp[k] = x[a++]; else tmp[k] = x[b++]; } for( int k = lo; k <= hi; k++ ) x[k] = tmp[k]; } 19 Elegant & brilliant… but not how I’d write it.

Mergesort (by Steve): void msort(int data[], int left, int right, int temp[]) { if (left >= right) return; int mid = (left+right)/2; msort(data, left, mid, temp); msort(data, mid, right, temp); merge(data, left, mid, right, temp); } void mergesort(int data[], int n) { int *temp = new int[n]; msort(data, 0, n-1, temp); delete[] temp; } 20

Merge (by Jon Bentley): void merge(int data[], int left, int mid, int right, int temp[]) { int frontL = left, frontR = mid+1; for(int dest = left; dest <= right; ++dest) { if (frontL > mid) { // left is empty? temp[dest] = data[frontR]; ++frontR; } else if (frontR > mid) { // right is empty? temp[dest] = data[frontL]; ++frontL; } else if (data[frontR] < data[frontL]) { temp[dest] = data[frontR]; // right elt ++frontR; // is smaller } else { temp[dest] = data[frontL]; // left elt ++frontL; // is smaller } } for( int k = left; k <= right; ++k ) data[k] = temp[k]; } 21

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 22

QuickSort In practice, one of the fastest sorting algorithms is Quicksort, developed in 1961 by C.A.R. Hoare. Comparison-based: examines elements by comparing them to other elements Divide-and-conquer: divides into “halves” (that may be very unequal) and recursively sorts 23

QuickSort algorithm Pick a pivot Reorder the list such that all elements < pivot are on the left, while all elements  pivot are on the right Recursively sort each side Are we missing a base case? 24

Partitioning The act of splitting up an array according to the pivot is called partitioning Consider the following: left partition right partition pivot 25

QuickSort Visually PPPPPPPP Sorted! 26

QuickSort (by Jon Bentley): void qsort(int x[], int lo, int hi) { int i, p; if (lo >= hi) return; p = lo; for( i=lo+1; i <= hi; i++ ) if( x[i] < x[lo] ) swap(x[++p], x[i]); swap(x[lo], x[p]); qsort(x, lo, p-1); qsort(x, p+1, hi); } void quicksort(int x[], int n) { qsort(x, 0, n-1); } 27 Elegant & brilliant… but still not how I’d write it.

QuickSort Example (using intuitive algorithm, not Bentley’s) (Pick first element as pivot, “scoot” elements to left/right.)

QuickSort: Complexity Recall that Quicksort is comparison based –Thus, the operations are comparisons In our partitioning task, we compared each element to the pivot –Thus, the total number of comparisons is N –As with MergeSort, if one of the partitions is about half (or any constant fraction of) the size of the array, complexity is  (n lg n). In the worst case, however, we end up with a partition with a 1 and n-1 split 29

QuickSort Visually: Worst case P P P P 30

QuickSort: Worst Case In the overall worst-case, this happens at every step… –Thus we have N comparisons in the first step –N-1 comparisons in the second step –N-2 comparisons in the third step –: –…or approximately n 2 31

QuickSort: Average Case (Intuition) Clearly pivot choice is important –It has a direct impact on the performance of the sort –Hence, QuickSort is fragile, or at least “attackable” So how do we pick a good pivot? 32

QuickSort: Average Case (Intuition) Let’s assume that pivot choice is random –Half the time the pivot will be from the centre half of the array –Thus at worst the split will be n/4 and 3n/4 33

QuickSort: Average Case (Intuition) We can apply this to the notion of a good split –Every “good” split: 2 partitions of size n/4 and 3n/4 Or divides N by 4/3 –Hence, we make up to log4/3(N) “good” splits Expected # of partitions is at most 2 * log 4/3 (N) –O(lgN) Given N comparisons at each partitioning step, we have  (N lg N) 34

Quicksort Complexity: How does it compare? N Insertion Sort Quicksort 10, sec 0.05 sec 20, sec0.11 sec 300, sec (1.25 hrs) 2.15 sec 35

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 36

How Do Quick, Merge, Heap, Insertion, and Selection Sort Compare? Complexity –Best case: Insert < Quick, Merge, Heap < Select –Average case: Quick, Merge, Heap < Insert, Select –Worst case: Merge, Heap < Quick, Insert, Select –Usually on “real” data: Quick < Merge < Heap < I/S –On very short lists: quadratic sorts may have an advantage (so, some quick/merge implementations “bottom out” to these as base cases) Some details depend on implementation! (E.g., an initial check whether the last elt of the left sublist is less than first of the right can make merge’s best case linear.) 37 (not asymptotic)

How Do Quick, Merge, Heap, Insertion, and Selection Sort Compare? Stability –Easily Made Stable: Insert, Merge (prefer the “left” of the two sorted sublists on ties) –Unstable: Heap –Challenging to Make Stable: Quick, Select Memory use: –Insert, Select, Heap < Quick < Merge 38 How much stack space does recursive QuickSort use? In the worst case? Could we make it better?

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 39

Complexity of Sorting Using Comparisons as a Problem Each comparison is a “choice point” in the algorithm. You can do one thing if the comparison is true and another if false. So, the whole algorithm is like a binary tree… …… sorted!z < cc < dsorted! a < da < b x < y …… yesno yesnoyesno yesnoyesno 40

Complexity of Sorting Using Comparisons as a Problem The algorithm spits out a (possibly different) sorted list at each leaf. What’s the maximum number of leaves? …… sorted!z < cc < dsorted! a < da < b x < y …… yesno yesnoyesno yesnoyesno 41

Complexity of Sorting Using Comparisons as a Problem There are n! possible permutations of a sorted list (i.e., input orders for a given set of input elements). How deep must the tree be to distinguish those input orderings? …… sorted!z < cc < dsorted! a < da < b x < y …… yesno yesnoyesno yesnoyesno 42

Complexity of Sorting Using Comparisons as a Problem If the tree is not at least lg(n!) deep, then there’s some pair of orderings I could feed the algorithm which the algorithm does not distinguish. So, it must not successfully sort one of those two orderings. …… sorted!z < cc < dsorted! a < da < b x < y …… yesno yesnoyesno yesnoyesno 43

Complexity of Sorting Using Comparisons as a Problem QED: The complexity of sorting using comparisons is  (n lg n) in the worst case, regardless of algorithm! In general, we can lower-bound but not upper-bound the complexity of problems. (Why not? Because I can give as crappy an algorithm as I please to solve any problem.) 44

Today’s Outline Categorizing/Comparing Sorting Algorithms –PQSorts as examples MergeSort QuickSort More Comparisons Complexity of Sorting 45

To Do Read: Epp Section 9.5 and KW Section 10.1, 10.4, and

Next Up B+-trees and Giant Branching Factors 47