Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Similar documents
Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Computer Science 331

1 The sorting problem

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

COMP250: Loop invariants

EECS 2011M: Fundamentals of Data Structures

COMP Analysis of Algorithms & Data Structures

Searching Algorithms/Time Analysis

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Data Structures and Algorithms

Data Structures and Algorithms Sorting

Lecture 5 Sorting Arrays

Lecture Notes on Sorting

Algorithms. Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms

Sorting Pearson Education, Inc. All rights reserved.

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

Data Structures and Algorithms CSE 465

Chapter 2: Complexity Analysis

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

The divide and conquer strategy has three basic parts. For a given problem of size n,

Data Structures and Algorithms Chapter 2

CS Algorithms and Complexity

Lecture Notes for Chapter 2: Getting Started

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

CSC 273 Data Structures

Lecture 2: Getting Started

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

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

Problem Set 4 Solutions

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

An Annotated Language

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Algorithms - Ch2 Sorting

Biostatistics 615/815 Statistical Computing

LINEAR INSERTION SORT

Data Structures and Algorithms. Part 2

Outline. Computer Science 331. Course Information. Assessment. Contact Information Assessment. Introduction to CPSC 331

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Outline. Binary Tree

Problem:Given a list of n orderable items (e.g., numbers, characters from some alphabet, character strings), rearrange them in nondecreasing order.

Lecture 19 Sorting Goodrich, Tamassia

COMP Analysis of Algorithms & Data Structures

Outline. Computer Science 331. Analysis of Prim's Algorithm

Lecture 6 Sorting and Searching

Sorting Algorithms. Biostatistics 615 Lecture 7

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

Sorting. Bubble Sort. Selection Sort

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Lecture 3. Brute Force

CHAPTER 9. GRAPHS 310. Figure 9.1 An undirected graph. Figure 9.2 A directed graph

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

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

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

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

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Order Analysis of Algorithms. Sorting problem

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

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.

Chapter 16. Greedy Algorithms

Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

Unit #3: Recursion, Induction, and Loop Invariants

Assertions & Verification & Example Loop Invariants Example Exam Questions

Stores a collection of elements each with an associated key value

Advanced Algorithms and Data Structures

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

Binary Search to find item in sorted array

Sorting & Growth of Functions

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

Lecture Notes on Quicksort

Assertions & Verification Example Exam Questions

Selection (deterministic & randomized): finding the median in linear time

COSC 311: ALGORITHMS HW1: SORTING

Lecture Notes on Linear Search

CS : Data Structures

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Lower bound for comparison-based sorting

CSE 2123 Sorting. Jeremy Morris

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

University of Waterloo CS240, Winter 2010 Assignment 2

Cpt S 122 Data Structures. Sorting

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

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

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

Transcription:

Outline Computer Science 331 Classical Sorting Algorithms Mike Jacobson Department of Computer Science University of Calgary Lecture #22 1 Introduction 2 3 4 5 Comparisons Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 1 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 2 / 33 The Sorting Problem Introduction Introduction Three Classical Algorithms Precondition: A: Array of length n, for some integer n 1, storing objects of some ordered type Postcondition: A: Elements have been permuted (reordered) but not replaced, in such a way that A[i] A[i + 1] for 0 i < n 1 Discussed today: three classical sorting algorithms Reasonably simple Work well on small arrays Each can be used to sort an array of size n using Θ(n 2 ) operations (comparisons and exchanges of elements) in the worst case None is a very good choice to sort large arrays: Asymptotically faster algorithms exist! Reference: Textbook, Section 10.1-10.5 Section 10.1 using Java s sorting functions Section 10.5 comparison of classical sorting algorithms Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 3 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 4 / 33

Pseudocode Idea: Repeatedly find i th -smallest element and exchange it with the element in location A[i] Result: After i th exchange, A[0], A[1],..., A[i 1] are the i smallest elements in the entire array, in sorted order Reference: Textbook, Section 10.2 for i from 0 to n 2 do min = i for j from i + 1 to n 1 do if A[j] < A[min] then min = j end if tmp = A[i]; A[i] = A[min]; A[min] = tmp {Swap} Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 5 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 6 / 33 Example Example (cont.) A: 2 6 3 1 4 Idea: find smallest element in A[i],..., A[4] for each i from 0 to n 1 i = 0 set min = 3 (A[3] = 1 is minimum of A[0],..., A[4]) swap A[0] and A[3] (A[0] sorted) A: 1 6 3 2 4 i = 1 set min = 3 (A[3] = 2 is minimum of A[1],..., A[4]) swap A[1] and A[3] (A[0], A[1] sorted) A: 1 2 3 6 4 i = 2 set min = 2 (A[2] = 3 is minimum of A[2],..., A[4]) swap A[2] and A[2] (A[0], A[1], A[2] sorted) A: 1 6 3 2 4 i = 3 set min = 4 (A[4] = 4 is minimum of A[3], A[4]) swap A[3] and A[4] (A[0], A[1], A[2], A[3] sorted) A: 1 2 3 4 6 Finished! A[0],..., A[4] sorted Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 7 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 8 / 33

Inner Loop: Semantics Inner Loop: Loop Invariant The inner loop is a for loop, which does the same thing as the following code (which includes a while loop): j = i + 1 while j n 1 do if (A[j] < A[min]) then min = j end if j = j + 1 end while We will supply a loop invariant and loop variant for the above while loop in order to analyze the behaviour of the for loop we used to generate it Loop Invariant: After k executions of the inner loop body, n, i, min N; n is the size of A First subarray (with size i) is sorted with smallest elements: 0 i n 2 A[h] A[h + 1] for 0 h i 2 if i > 0 then A[i 1] A[h] for i h n 1 Searching for the next-smallest element: i + 1 j n and j = i + 1 + k i min < j A[min] A[h] for i h < j Entries of A have been reordered; otherwise unchanged Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 9 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 10 / 33 Inner Loop: Interpretation of the Loop Invariant Application of the Loop Invariant all > A[i 1] {}}{ A: i 1 i j 1 j } {{ } sorted } {{ } A[min] smallest After k executions of the loop body, the following hold: A[0],..., A[i 1] are sorted A[l] A[i 1] for i l < n i min < j and A[min] A[h] for i h < j entries of A have been reordered, otherwise unchanged Loop invariant and failure of the loop test ensures that j = n immediately after the final execution of the inner loop body This, and the loop invariant, ensures that i min < n and that A[min] A[l] for all l such that i l < n The loop invariant also ensures that A[min] A[h] for all h such that 0 h < i In other words, A[min] is the value that should be moved into position A[i] Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 11 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 12 / 33

Inner Loop: Loop Variant and Application Outer Loop: Semantics Loop Variant: f (n, i, j) = n j decreasing integer function when f (n, i, j) = 0 we have j = n and the loop terminates Application: initial value is j = i + 1 worst-case number of iterations is f (n, i, i + 1) = n (i + 1) = n 1 i The outer loop is a for loop whose index variable i has values from 0 to n 2, inclusive This does the same thing as a sequence of statements including an initialization statement, i = 0 a while loop with test i n 2 whose body consists of the body of the for loop, together with a final statement i = i + 1 We will provide a loop invariant and a loop variant for this while loop in order to analyze the given for loop Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 13 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 14 / 33 Outer Loop: Loop Invariant and Loop Variant of Loop Invariant: After k executions of the outer loop body, 0 i n 1 and i = k A[h] A[h + 1] for 0 h < i if k > 0, A[i 1] A[h] for i h < n Entries of A have been reordered; otherwise unchanged Thus: A[0],..., A[i 1] are sorted and are the i smallest elements in A Loop Variant: f (n, i) = n 1 i decreasing integer function when f (n, i) = 0 we have i = n 1 and the loop terminates worst-case number of iterations is f (n, 0) = n 1 Worst-case: Θ(n 2 ) steps inner loop iterates n 1 i times (constant steps per iteration) outer loop iterates n 1 times total number of steps is at most n 2 n 2 c 0 + c 1 (n 1 i) = c 0 + c 1 (n 1) 2 c 1 i Θ(n 2 ) i=0 i=0 Conclusion: Worst-case running time is in O(n 2 ). Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 15 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 16 / 33

of, Concluded Best-Case: Also in Ω(n 2 ) : Both loops are for loops and a positive number of steps is used on each execution of the inner loop body Total number of steps is therefore at least n 2 ĉ 0 + ĉ 1 (n 1 i) Ω(n 2 ) i=0 Conclusion: Every application of this algorithm to sort an array of length n uses Θ(n 2 ) steps Idea: Sort progressively larger subarrays n 1 stages, for i = 1, 2,..., n 1 At the end of the i th stage Entries originally in locations A[0], A[1],..., A[i] have been reordered and are now sorted Entries in locations A[i + 1], A[i + 2],..., A[n 1] have not yet been examined or moved Reference: Textbook, Section 10.4 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 17 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 18 / 33 Pseudocode Example A: 2 6 3 1 4 for i from 1 to n 1 do j = i while ((j > 0) and (A[j] < A[j 1])) do tmp = A[j]; A[j] = A[j 1]; A[j 1] = tmp {Swap} j = j 1 end while Idea: insert A[i] in the correct position in A[0],..., A[i 1] initially, i = 0 and A[0] = 2 is sorted i = 1 no swaps A[0], A[1] sorted A: 2 6 3 1 4 i = 2 swap A[2] & A[1] A[0], A[1], A[2] sorted A: 2 3 6 1 4 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 19 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 20 / 33

Example (cont.) Inner Loop: Loop Invariant i = 3 swap A[3] & A[2], swap A[2] & A[1], swap A[1] & A[0] A[0], A[1], A[2], A[3] sorted A: 1 2 3 6 4 i = 4 swap A[4] & A[3] A[0], A[1], A[2], A[3], A[4] sorted A: 1 2 3 4 6 Finished! A[0],..., A[4] sorted Consider some execution of the outer loop body, and the use of the inner loop as part of it. Loop Invariant: After k executions of the inner loop body, i, j, n N; n is the size of A 1 i < n and 0 j i j = i k A[h] A[h + 1] for 0 h j 2 and for j h < i if j > 0 and j < i then A[j 1] A[j + 1] Entries of A have been reordered; otherwise unchanged Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 21 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 22 / 33 Inner Loop: Interpretation of Loop Invariant Inner Loop: Loop Variant and Application A: j 1 j j + 1 i } {{ } sorted } {{ } sorted After k executions of the loop body, the following hold: A[0],..., A[j 1] are sorted A[j],..., A[i] are sorted A[j 1] A[j + 1], so that A[0],..., A[i] are sorted if A[j 1] A[j] Loop Variant: f (n, i, j) = j decreasing integer function when f (n, i, j) = 0 we have j = 0 and the loop terminates Application: initial value is i worst-case number of iterations is i Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 23 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 24 / 33

Outer Loop: Semantics Outer Loop Once again, the outer for loop can be rewritten as a while loop for analysis. Since the inner loop is already a while loop, the new outer while loop would be as follows. i = 1 while i n 1 do j = i Inner loop of original program i = i + 1 end while This program will be analyzed in order establish the correctness and efficiency of the original one. Loop Invariant: After k executions of the outer loop body, 1 i n and i = k + 1 A[h] A[h + 1] for 0 h < i 2 Entries of A have been reordered; otherwise unchanged. Thus, A[0],..., A[i 1] are sorted, for i = k + 1, after k iterations. after n 1 iterations, A is sorted Loop Variant: f (n, i) = n i number of iterations is f (n, 1) = n 1 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 25 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 26 / 33 of, Concluded of, Concluded Worst-case: Θ(n 2 ) steps inner loop iterates i times (constant steps per iteration) outer loop iterates n 1 times total number of steps is n 1 (n 1)(n 2) c 0 + c 1 i = c 0 + c 1 2 i=1 Conclusion: Worst-case running time is in O(n 2 ). Θ(n 2 ) Worst-Case, Continued: For every integer n 1 consider the operation on this algorithm on an input array A such that the length of A is n the entries of A are distinct A is sorted in decreasing order, instead of increasing order It is possible to show that the algorithm uses Ω(n 2 ) steps on this input array. Conclusion: The worst-case running time is in Θ(n 2 ). Best-Case: Θ(n) steps are used in the best case. Proof: Exercise. Consider an array whose entries are already sorted as part of this. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 27 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 28 / 33

Pseudocode Idea: Similar, in some ways, to Repeatedly sweep from right to left over the unsorted (rightmost) portion of the array, keeping the smallest element found and moving it to the left Result: After the i th stage, A[0], A[1],..., A[i 1] are the i smallest elements in the entire array, in sorted order for i from 0 to n 2 do for j from n 2 down to i do if A[j] > A[j + 1] then tmp = A[j]; A[j] = A[j + 1]; A[j + 1] = tmp {Swap} end if Reference: Textbook, Section 10.3 (variation of this idea) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 29 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 30 / 33 of Inner Loop of Outer Loop Exercise! Rewrite the inner loop as an equivalent while loop (preceded by an initialization statement) Try to use your understanding of what the inner loop does to find a loop invariant. This should include enough information so that it can be proved to hold (probably using mathematical induction) and so that it can be used to establish correctness of the outer loop. Try to find a loop variant for the inner loop as well. Begin, as usual, by rewriting this loop as an equivalent while loop (preceded by an initialization statement) The loop invariant and loop variant given for the outer loop of the algorithm can be used here, as well. Proving this is different, since the details of the inner loops of these two algorithms are quite different. The application of the loop invariant and loop variant to establish correctness are then much the same as for the algorithm. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 31 / 33 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 32 / 33

Comparisons Comparisons All three algorithms have worst-case complexity Θ(n 2 ) Selection sort only swaps O(n) elements, even in the worst case. This is an advantage when exchanges are more expensive than comparisons. On the other hand, Insertion sort has the best best case complexity. It also performs well if the input as already partly sorted. Bubble sort is generally not used in practice. Note: Asymptotically faster algorithms exist and will be presented next. These asymptotically faster algorithms are better choices when the input size is large and worst-case performance is critical. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #22 33 / 33