Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Similar documents
Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Sorting Goodrich, Tamassia Sorting 1

Quick-Sort. Quick-Sort 1

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Lecture 19 Sorting Goodrich, Tamassia

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

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Sorting. Data structures and Algorithms

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Merge Sort Goodrich, Tamassia Merge Sort 1

QuickSort

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

SORTING AND SELECTION

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

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Sorting Algorithms. + Analysis of the Sorting Algorithms

Comparison Sorts. Chapter 9.4, 12.1, 12.2

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

Lecture Notes on Quicksort

Lecture Notes on Quicksort

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

SORTING, SETS, AND SELECTION

CSE 373: Data Structures and Algorithms

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

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

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

Recursive Sorts. Recursive Sorts. Divide-and-Conquer. Divide-and-Conquer. Divide-and-conquer paradigm:

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Merge Sort

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.

Programming II (CS300)

DATA STRUCTURES AND ALGORITHMS

Question 7.11 Show how heapsort processes the input:

Measuring algorithm efficiency

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

COMP Data Structures

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

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

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

We can use a max-heap to sort data.

CSE 331 Software Design and Implementation. Lecture 3 Loop Reasoning

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Programming II (CS300)

LECTURE 17. Array Searching and Sorting

12/30/2013 S. NALINI,AP/CSE

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

Data Structures and Algorithms " Sorting!

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

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

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Backward Reasoning: Rule for Assignment. Backward Reasoning: Rule for Sequence. Simple Example. Hoare Logic, continued Reasoning About Loops

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Outline and Reading. Quick-Sort. Partition. Quick-Sort. Quick-Sort Tree. Execution Example

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

Sorting and Selection

EECS 2011M: Fundamentals of Data Structures

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

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

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

Divide and Conquer Sorting Algorithms and Noncomparison-based

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

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

Divide and Conquer Algorithms: Advanced Sorting

Faster Sorting Methods

Programming II (CS300)

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

C/C++ Programming Lecture 18 Name:

Sorting. Divide-and-Conquer 1

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

// 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];

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

BM267 - Introduction to Data Structures

CS102 Sorting - Part 2

Better sorting algorithms (Weiss chapter )

Pseudo code of algorithms are to be read by.

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

1. The Sets ADT. 1. The Sets ADT. 1. The Sets ADT 11/22/2011. Class Assignment Set in STL

ECE368 Exam 2 Spring 2016

(Refer Slide Time: 01.26)

cmpt-225 Sorting Part two

Active Learning: Sorting

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

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

Mergesort again. 1. Split the list into two equal parts

LINEAR INSERTION SORT

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Transcription:

Plan of the lecture Quick-sort Lower bounds on comparison sorting Correctness of programs (loop invariants) Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 Lecture 16 1 Lecture 16 2 Quick-Sort ( 10.2) Partition of lists (or using extra workspace) Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recur: sort L and G Conquer: join L, E and G L x x E x G We partition an input sequence as follows: We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick-sort takes O(n) time Lecture 16 3 Lecture 16 4 Partitioning arrays Perform the partition using two indices to split S into L and E U G (a similar method can split E U G into E and G). j k 3 2 5 1 0 7 3 5 9 2 7 9 8 9 7 6 9 (pivot = 6) Repeat until j and k cross: Scan j to the right until finding an element > x. Scan k to the left until finding an element < x. Swap elements at indices j and k j k 3 2 5 1 0 7 3 5 9 2 7 9 8 9 7 6 9 Quick-Sort Tree An execution of quick-sort is depicted by a binary tree Each node represents a recursive call of quick-sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 Lecture 16 5 Lecture 16 6 1

Execution Example Pivot selection Partition, recursive call, pivot selection 7 2 9 4 2 4 7 9 2 4 3 1 2 4 7 9 2 2 9 4 4 9 2 2 9 4 4 9 Lecture 16 7 Lecture 16 8 Partition, recursive call, base case 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 Recursive call,, base case, join 2 4 3 1 2 4 7 1 1 9 4 4 9 Lecture 16 9 Lecture 16 10 Recursive call, pivot selection Partition,, recursive call, base case 7 9 7 1 1 3 8 6 7 9 7 1 1 3 8 6 8 8 9 9 8 8 9 9 Lecture 16 11 Lecture 16 12 2

Join, join 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 7 9 7 17 7 9 Worst-case Running Time The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n 1 and the other has size 0 The running time is proportional to the sum n + (n 1) + + 2 + 1 Thus, the worst-case running time of quick-sort is O(n 2 ) depth time 0 n 8 8 9 9 1 n 1 n 1 1 Lecture 16 13 Lecture 16 14 Best-case Running Time The best case for quick-sort occurs when the pivot is the median element The L and G parts are equal the sequence is split in halfs, like in merge sort Thus, the best-case running time of quick-sort is O(nlog n) Average-case Running Time The average case for quick-sort: half of the times, the pivot is roughly in the middle Thus, the average-case running time of quick-sort is O(n log n) again Detailed proof in the textbook Lecture 16 15 Lecture 16 16 Summary of Sorting Algorithms Lower bound for comparison sorting Algorithm selection-sort insertion-sort quick-sort heap-sort merge-sort Time O(n 2 ) O(n 2 ) O(n log n) expected O(n log n) O(n log n) Notes in-place (no extra memory) slow (good for small inputs) in-place (no extra memory) slow (good for small inputs) in-place (+stack), randomized fast (good for large inputs) in-place (if heapify the sequence to be sorted) fast (good for large inputs) sequential data access fast (good for huge inputs) Can model sorting which depends on comparisons between elements as a binary decision tree. At each node, a comparison between two elements is made; there are two possible outcomes and we find out a bit more about the correct order of items in the array. Finally arrive at full information about the correct order of the items in the array. Lecture 16 17 Lecture 16 18 3

Comparison sorting Correct order is... a 2 < a 3? yes a 1,,a n : don t know what the correct order is. a 1 < a 2? yes... (a 1,,a n ) (a 2, a 1,...,a n ) (total of n! possibilities) no Comparison sorting If a binary tree has n! leaves than the minimal number of levels (assuming the tree is perfect) is log n! +1. This shows that O(n log n) sorting algorithms are essentially optimal (log 2 n! is not equal to n log 2 n but has the same growth rate modulo some hairy constants). Lecture 16 19 Lecture 16 20 Correctness of algorithms Two issues: Given an algorithm, prove that it is correct (always achieves the intended result, e.g. a sorted array). Design an algorithm with intended properties from specification (even more difficult) Additional reading Goodrich and Tamassia, Chapter 3.5 Frank M. Carrano, Janet J. Prichard. Data abstraction and problem solving with Java. Addison Wesley Longman, 2001. Chapter 1, Problem solving and software engineering (on verification). Duane A. Bailey. Data structures in Java for the principled programmer. McGraw-Hill 1999. Chapter 2, Comments, conditions and assertions (pre- and postconditions). Roland Backhouse. Program Construction : Calculating Implementations from Specifications. John Wiley & Sons 2003. Lecture 16 21 Lecture 16 22 Disproving correctness Proving correctness Just one counterexample is enough Testing may fail to discover a bug Formulate precisely the property which has to hold If necessary, formulate relevant properties for smaller parts of an algorithm : program assertions Lecture 16 23 Lecture 16 24 4

Assertions Hoare triples Assertion: claim about values of program variables before or after a statement or a group of statements is executed Typical assertions: Precondition (usually of a method): what we expect to hold before the method is executed. Postcondition: what holds after the method is executed. Lecture 16 25 {P} S {Q}: P precondition, S statements, Q postcondition. Meaning: provided P holds before S is executed, then after S is executed, Q holds. for example: {x < 10} x = x + 20 {x < 30} {x < y} while (x < y) x++ {x=y} For a small programming language, can provide axioms for every construct in the language and derive postconditions using axioms Lecture 16 26 Assignment axiom If the only programming construct was assignment, here is an axiom to verify all programs: {Q(e substituted for x)} x = e {Q} For example, if want to prove {x < 10} x = x + 20 {x < 30} then the assignment axiom gives {x+20 < 30} x = x + 20 {x < 30} and from extra knowledge about math etc we derive that {x+20 < 30} is equivalent to {x < 10}. Example In the programs we usually write there are lots of constructs and they also use other people s code. Less formal approach (but good practice): write pre- and postconditions for significant chunks of code/methods. Example: code in Bailey s book. Lecture 16 27 Lecture 16 28 Proving correctness Loop Invariants To prove that an algorithm is correct: Determine preconditions and postconditions for the whole algorithm. Cascade statement assertions together, so that postconditions for one provide preconditions for the next. Prove correctness of individual statements. Hence show that executing algorithm with stated preconditions terminates and leads to stated post-conditions. Lecture 16 29 Assertions for loops are difficult, because loops may be executed many times over, with slightly different assertions holding before and after each iteration. Focus on those assertions that remain constant between iterations. Known as loop invariants: true before and after each iteration through a loop. Lecture 16 30 5

Example Correctness of loops pos_greatest = 0; for (int j = 0; j < =i; j++) { if( arr[j] > arr[pos_greatest]) { pos_greatest = j; } } Invariant: pos_greatest is the index of the largest array element between 0 and j. (More formally, for all k such that 0 k < j, arr[k] arr[pos_greatest].) Lecture 16 31 To prove correctness of a while loop (or: that assertion A holds after the loop terminates): Prove that the loop eventually terminates (by finding the bound function for the loop) Find a suitable invariant (there are infinitely many invariants for each loop, most of them useless) Prove that A is true after last iteration (usually by substituting the state in which the loop terminates in the invariant) Lecture 16 32 Partition algorithm small = l; // set at the left border of the // range large = r; // set at the right border where the // pivot sits while(small < large) { if (arr[small] < pivot) small++; else { large--; temp = arr[small]; arr[small] = arr[large]; arr[large] = temp; } } temp = arr[r]; arr[r] = arr[large]; arr[large] = temp; return large; Postcondition for the partition public int partition(int[] arr, int l, int r) // post: returns and integer k such that // for all indices i such that l<=i<k, // arr[i]<arr[k] and // for all indices i such that k<=i<r // arr[i]>=arr[k] values < pivot pivot values pivot 0 1 2 k k+1 r Lecture 16 33 Lecture 16 34 Loop invariant for partition for all indices i, if l i < small, then arr[i] < pivot if large i < r then arr[i] pivot Bound function for partition bound function = large - small decreases by 1 at every step; the loop terminates when it is equal to 0 (small = large). values < pivot unsorted stuff values pivot 0 1 small large r Lecture 16 35 Lecture 16 36 6

After the loop When large = small, the invariant: if l i < small, then arr[i] < pivot if large i < r then arr[i] pivot becomes (substitute large for small): if l i < large, then arr[i] < pivot if large i < r then arr[i] pivot After the pivot is swapped, pivot = arr[large]. Partition algorithm Assertion 1: l < r; pivot is arr[r] while(small < large) { if (arr[small] < pivot) small++; else { large--; temp = arr[small]; arr[small] = arr[large]; arr[large] = temp;}} Assertion 2: pivot is arr[r]; if l i < large, then arr[i] < pivot; if large i < r then arr[i] pivot temp = arr[r]; arr[r] = arr[large]; arr[large] = temp; Assertion 3: arr[large] is the pivot return large; Lecture 16 37 Lecture 16 38 Informal coursework (tutorials from the 6 th of December) Given that l < r in the partition method, which of the following are loop invariants of the while loop: 1) small < r 2) small < large 3) small large 4) for all i such that l i < small, arr[i] < pivot 5) for all j such that large j r, arr[j] pivot 6) for all j such that large < j r, arr[j] pivot Informal coursework 2 Prove correctness of selection sort: void selectionsort(int arr[], int len){ int i,j,temp,pos_greatest; for( i = len - 1; i > 0; i--){ pos_greatest = 0; for(j = 0; j <= i; j++){ if(arr[j] > arr[pos_greatest]) pos_greatest = j; }//end inner for loop temp = arr[i]; arr[i] = arr[pos_greatest]; arr[pos_greatest] = temp; }} Lecture 16 39 Lecture 16 40 7