SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

Similar documents
"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

SORTING 9/26/18. Prelim 1. Prelim 1. Why Sorting? InsertionSort. Some Sorting Algorithms. Tonight!!!! Two Sessions:

Announcements SORTING. Prelim 1. Announcements. A3 Comments 9/26/17. This semester s event is on Saturday, November 4 Apply to be a teacher!

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2, CS2110. SOLUTION

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

ASYMPTOTIC COMPLEXITY

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

ASYMPTOTIC COMPLEXITY

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

InserDonSort. InserDonSort. SelecDonSort. MergeSort. Divide & Conquer? 9/27/12

COMP Data Structures

Sorting is a problem for which we can prove a non-trivial lower bound.

Total Score /1 /20 /41 /15 /23 Grader

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Programming II (CS300)

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Unit-2 Divide and conquer 2016

Question 7.11 Show how heapsort processes the input:

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS61BL. Lecture 5: Graphs Sorting

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS 1110, LAB 13: SEQUENCE ALGORITHMS

Module 3: Sorting and Randomized Algorithms

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Divide and Conquer Sorting Algorithms and Noncomparison-based

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CS126 Final Exam Review

COSC242 Lecture 7 Mergesort and Quicksort

Sorting. Bringing Order to the World

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Sorting is ordering a list of objects. Here are some sorting algorithms

Lecture Notes on Quicksort

Randomized Algorithms, Quicksort and Randomized Selection

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CmpSci 187: Programming with Data Structures Spring 2015

Intro to Algorithms. Professor Kevin Gold

CSE 2123 Sorting. Jeremy Morris

Basic Techniques. Recursion. Today: Later on: Recursive methods who call themselves directly or indirectly.

Lecture 5: Sorting Part A

Chapter 7 Sorting. Terminology. Selection Sort

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Lecture Notes on Quicksort

Sorting Pearson Education, Inc. All rights reserved.

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Total Points: 60. Duration: 1hr

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Lecture 19 Sorting Goodrich, Tamassia

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Quicksort (Weiss chapter 8.6)

Arrays, Vectors Searching, Sorting

CSC Design and Analysis of Algorithms

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

CS 1110: Introduction to Computing Using Python Loop Invariants

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

BM267 - Introduction to Data Structures

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Lecture 8: Mergesort / Quicksort Steven Skiena

O(n): printing a list of n items to the screen, looking at each item once.

Unit 10: Sorting/Searching/Recursion

Transcription:

1 SORTING Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity See lecture notes page, row in table for this lecture, for file searchsortalgorithms.zip Lecture 11 CS2110 Spring 2017

A3 and Prelim 2 379/607 (62%) people got 65/65 for correctness on A3 558/607 (92%) got at least 60/65 for correctness on A3 Prelim: Next Tuesday evening, March 14 Read the Exams page on course website to determine when you take the prelim (5:30 or 7:30) and what to do if you have a conflict. If necessary, complete CMS assignment P1Conflict by the end of Wednesday (tomorrow). So far, only 15 people filled it out!

InsertionSort 3 pre: b 0 b.length? post: b 0 b.length sorted inv: b 0 i b.length sorted? or: inv: b b[0..i-1] is sorted 0 i b.length processed? A loop that processes elements of an array in increasing order has this invariant for (int i= 0; i < b.length; i= i+1) { maintain invariant

Each iteration, i= i+1; How to keep inv true? 4 inv: e.g. b b b 0 i b.length sorted? 0 i b.length 2 5 5 5 7 3? 0 i b.length 2 3 5 5 5 7? Push b[i] down to its shortest position in b[0..i] Will take time proportional to the number of swaps needed

What to do in each iteration? 5 inv: e.g. b b 0 i b.length sorted? 0 i b.length 2 5 5 5 7 3? Loop body (inv true before and after) b 2 5 5 5 3 7? 2 5 5 3 5 7? 2 5 3 5 5 7? 2 3 5 5 5 7? 0 i b.length 2 3 5 5 5 7? Push b[i] to its sorted position in b[0..i], then increase i

InsertionSort 6 // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 0; i < b.length; i= i+1) { Push b[i] down to its sorted position in b[0..i] Many people sort cards this way Works well when input is nearly sorted Note English statement in body. Abstraction. Says what to do, not how. This is the best way to present it. We expect you to present it this way when asked. Later, can show how to implement that with an inner loop

Push b[i] down 7 // Q: b[0..i-1] is sorted // Push b[i] down to its sorted position in b[0..i] int k= i; while (k > 0 && b[k] < b[k-1]) { Swap b[k] and b[k-1] k= k 1; // R: b[0..i] is sorted invariant P: b[0..i] is sorted except that b[k] may be < b[k-1] k start? stop? progress? maintain invariant? 2 5 3 5 5 7? example i

8 How to write nested loops // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 0; i < b.length; i= i+1) { while (k > 0 && b[k] < b[k-1]) { Push b[i] down to its sorted k= k 1; position in b[0..i] Present algorithm like this If you are going to show implementation, put in WHAT IT DOES as a comment // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 0; i < b.length; i= i+1) { //Push b[i] down to its sorted //position in b[0..i] int k= i; while (k > 0 && b[k] < b[k-1]) { swap b[k] and b[k-1]; k= k-1;

InsertionSort 9 // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 0; i < b.length; i= i+1) { Push b[i] down to its sorted position in b[0..i] Pushing b[i] down can take i swaps. Worst case takes 1 + 2 + 3 + n-1 = (n-1)*n/2 Swaps. Let n = b.length Worst-case: O(n 2 ) (reverse-sorted input) Best-case: O(n) (sorted input) Expected case: O(n 2 )

SelectionSort 10 pre: b inv: b 0 b.length? post: b 0 b.length sorted 0 i b.length sorted, <= b[i..] >= b[0..i-1] Additional term in invariant Keep invariant true while making progress? 0 i b.length e.g.: b 1 2 3 4 5 6 9 9 9 7 8 6 9 Increasing i by 1 keeps inv true only if b[i] is min of b[i..]

SelectionSort 11 //sort b[], an array of int Another common way for // inv: b[0..i-1] sorted AND people to sort cards // b[0..i-1] <= b[i..] Runtime for (int i= 0; i < b.length; i= i+1) { with n = b.length int m= index of minimum of b[i..]; Worst-case O(n 2 ) Swap b[i] and b[m]; Best-case O(n 2 ) Expected-case O(n 2 ) b 0 i length sorted, smaller values larger values Each iteration, swap min value of this section into b[i]

Swapping b[i] and b[m] 12 // Swap b[i] and b[m] int t= b[i]; b[i]= b[m]; b[m]= t;

Partition algorithm of quicksort 13 pre: h h+1 x? k x is called the pivot Swap array values around until b[h..k] looks like this: post: h j k <= x x >= x

14 pivot 20 31 24 19 45 56 4 20 5 72 14 99 partition j 19 4 5 14 20 31 24 45 56 20 72 99 Not yet sorted Not yet sorted these can be in any order these can be in any order The 20 could be in the other partition

Partition algorithm 15 pre: b h h+1 x? k h j k post: b <= x x >= x Combine pre and post to get an invariant h j t k b <= x x? >= x invariant needs at least 4 sections

Partition algorithm 16 b h j t k <= x x? >= x j= h; t= k; while (j < t) { if (b[j+1] <= b[j]) { Swap b[j+1] and b[j]; j= j+1; else { Swap b[j+1] and b[t]; t= t-1; Takes linear time: O(k+1-h) Initially, with j = h and t = k, this diagram looks like the start diagram Terminate when j = t, so the? segment is empty, so diagram looks like result diagram

QuickSort procedure 17 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { if (b[h..k] has < 2 elements) return; Base case int j= partition(b, h, k); // We know b[h..j 1] <= b[j] <= b[j+1..k] // Sort b[h..j-1] and b[j+1..k] QS(b, h, j-1); QS(b, j+1, k); Function does the partition algorithm and returns position j of pivot h j k <= x x >= x

18 QuickSort Quicksort developed by Sir Tony Hoare (he was knighted by the Queen of England for his contributions to education and CS). 83 years old. Developed Quicksort in 1958. But he could not explain it to his colleague, so he gave up on it. Later, he saw a draft of the new language Algol 58 (which became Algol 60). It had recursive procedures. First time in a procedural programming language. Ah!, he said. I know how to write it better now. 15 minutes later, his colleague also understood it.

Tony Hoare 19 Speaking in Olin 155 in 2004

20 Tony Hoare Elaine Gries, Edsger and Ria Dijkstra, Tony and Jill Hoare 1980s.

21 Worst case quicksort: pivot always smallest value j n x0 >= x0 partioning at depth 0 j x0 x1 >= x1 partioning at depth 1 j x0 x1 x2 >= x2 partioning at depth 2 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { if (b[h..k] has < 2 elements) return; int j= partition(b, h, k); QS(b, h, j-1); QS(b, j+1, k); Depth of recursion: O(n) Processing at depth i: O(n-i) O(n*n)

Best case quicksort: pivot always middle value 22 0 j n <= x0 x0 >= x0 depth 0. 1 segment of size ~n to partition. <=x1 x1 >= x1 x0 <=x2 x2 >=x2 Depth 2. 2 segments of size ~n/2 to partition. Depth 3. 4 segments of size ~n/4 to partition. Max depth: O(log n). Time to partition on each level: O(n) Total time: O(n log n). Average time for Quicksort: n log n. Difficult calculation

23 QuickSort complexity to sort array of length n Time complexity Worst-case: O(n*n) /** Sort b[h..k]. */ Average-case: O(n log n) public static void QS(int[] b, int h, int k) { if (b[h..k] has < 2 elements) return; int j= partition(b, h, k); // We know b[h..j 1] <= b[j] <= b[j+1..k] // Sort b[h..j-1] and b[j+1..k] QS(b, h, j-1); QS(b, j+1, k); Worst-case space:? What s depth of recursion? Worst-case space: O(n)! --depth of recursion can be n Can rewrite it to have space O(log n) Show this at end of lecture if we have time

Partition. Key issue. How to choose pivot 24 pre: post: b b h h k x? h j k <= x x >= x Choosing pivot Ideal pivot: the median, since it splits array in half But computing is O(n), quite complicated Popular heuristics: Use w first array value (not so good) w middle array value (not so good) w Choose a random element (not so good) w median of first, middle, last, values (often used)!

Merge two adjacent sorted segments 25 /* Sort b[h..k]. Precondition: b[h..t] and b[t+1..k] are sorted. */ public static merge(int[] b, int h, int t, int k) { h t k b 4 7 7 8 9 3 4 7 8 h t k sorted sorted b 3 4 4 7 7 7 8 8 9 h merged, sorted k

Merge two adjacent sorted segments 26 /* Sort b[h..k]. Precondition: b[h..t] and b[t+1..k] are sorted. */ public static merge(int[] b, int h, int t, int k) { Copy b[h..t] into a new array c; Merge c and b[t+1..k] into b[h..k]; Runs in time linear in size of b[h..k]. Look at this method in file searchsortalgorithms.zip found in row for lecture on Lecture notes page of course website h t k h sorted merged, sorted sorted k

Merge two adjacent sorted segments 27 // Merge sorted c and b[t+1..k] into b[h..k] 0 t-h h t k pre: c x b? y x, y are sorted post: b h k x and y, sorted 0 i c.length invariant: c head of x tail of x b[h..u-1] c[i..] h u v k b[h..u-1] b[v..k] b? tail of y head of x and head of y, sorted

Mergesort 28 /** Sort b[h..k] */ public static void mergesort(int[] b, int h, int k]) { if (size b[h..k] < 2) return; int t= (h+k)/2; mergesort(b, h, t); mergesort(b, t+1, k); merge(b, h, t, k); h t k h sorted merged, sorted sorted k

Mergesort 29 /** Sort b[h..k] */ public static void mergesort( int[] b, int h, int k]) { if (size b[h..k] < 2) return; int t= (h+k)/2; mergesort(b, h, t); mergesort(b, t+1, k); merge(b, h, t, k); Let n = size of b[h..k] Merge: time proportional to n Depth of recursion: log n Can therefore show (later) that time taken is proportional to n log n But space is also proportional to n

QuickSort versus MergeSort 30 /** Sort b[h..k] */ public static void QS (int[] b, int h, int k) { if (k h < 1) return; int j= partition(b, h, k); QS(b, h, j-1); QS(b, j+1, k); /** Sort b[h..k] */ public static void MS (int[] b, int h, int k) { if (k h < 1) return; MS(b, h, (h+k)/2); MS(b, (h+k)/2 + 1, k); merge(b, h, (h+k)/2, k); One processes the array then recurses. One recurses then processes the array.

Analysis of Matrix Multiplication 31 Multiply n-by-n matrices A and B: Convention, matrix problems measured in terms of n, the number of rows, columns Input size is really 2n 2, not n Worst-case time: O(n 3 ) Expected-case time:o(n 3 ) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { throw new Exception(); for (i = 0; i < n; i++) for (j = 0; j < n; j++) { c[i][j] = 0; for (k = 0; k < n; k++) c[i][j] += a[i][k]*b[k][j];

32 An aside. Will not be tested. Lower Bound for Comparison Sorting Goal: Determine minimum time required to sort n items Note: we want worst-case, not best-case time Best-case doesn t tell us much. E.g. Insertion Sort takes O(n) time on alreadysorted input Want to know worst-case time for best possible algorithm How can we prove anything about the best possible algorithm? Want to find characteristics that are common to all sorting algorithms Limit attention to comparisonbased algorithms and try to count number of comparisons

33 An aside. Will not be tested. Lower Bound for Comparison Sorting Comparison-based algorithms make decisions based on comparison of data elements Gives a comparison tree If algorithm fails to terminate for some input, comparison tree is infinite Height of comparison tree represents worst-case number of comparisons for that algorithm Can show: Any correct comparisonbased algorithm must make at least n log n comparisons in the worst case no a[i] < a[j] yes

34 An aside. Will not be tested. Lower Bound for Comparison Sorting Say we have a correct comparison-based algorithm Suppose we want to sort the elements in an array b[] Assume the elements of b[] are distinct Any permutation of the elements is initially possible When done, b[] is sorted But the algorithm could not have taken the same path in the comparison tree on different input permutations

35 An aside. Will not be tested. Lower Bound for Comparison Sorting How many input permutations are possible? n! ~ 2 n log n For a comparison-based sorting algorithm to be correct, it must have at least that many leaves in its comparison tree To have at least n! ~ 2 n log n leaves, it must have height at least n log n (since it is only binary branching, the number of nodes at most doubles at every depth) Therefore its longest path must be of length at least n log n, and that is its worst-case running time

Quicksort with logarithmic space 36 Problem is that if the pivot value is always the smallest (or always the largest), the depth of recursion is the size of the array to sort. Eliminate this problem by doing some of it iteratively and some recursively

Quicksort with logarithmic space 37 Problem is that if the pivot value is always the smallest (or always the largest), the depth of recursion is the size of the array to sort. Eliminate this problem by doing some of it iteratively and some recursively. We may show you this later. Not today! It s on the next two slides. You do not have to study this for the prelim!

QuickSort with logarithmic space 38 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // invariant b[h..k] is sorted if b[h1..k1] is sorted while (b[h1..k1] has more than 1 element) { Reduce the size of b[h1..k1], keeping inv true

QuickSort with logarithmic space 39 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // invariant b[h..k] is sorted if b[h1..k1] is sorted while (b[h1..k1] has more than 1 element) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; else {QS(b, j+1, k1); k1= j-1; Only the smaller segment is sorted recursively. If b[h1..k1] has size n, the smaller segment has size < n/2. Therefore, depth of recursion is at most log n