Sorting Goodrich, Tamassia Sorting 1

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

Quick-Sort. Quick-Sort 1

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

Sorting. Data structures and Algorithms

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

Merge Sort Goodrich, Tamassia Merge Sort 1

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

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

Lecture 19 Sorting Goodrich, Tamassia

Sorting. Divide-and-Conquer 1

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

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

SORTING, SETS, AND SELECTION

SORTING AND SELECTION

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Data Structures and Algorithms " Sorting!

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

Merge Sort

QuickSort

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

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

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

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

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

Sorting and Selection

We can use a max-heap to sort data.

Merge Sort Goodrich, Tamassia. Merge Sort 1

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

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

Fast Sorting and Selection. A Lower Bound for Worst Case

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Introduction to Computers and Programming. Today

CHAPTER 11 SETS, AND SELECTION

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

COMP 352 FALL Tutorial 10

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

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.

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

Programming II (CS300)

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

CS Divide and Conquer

Programming II (CS300)

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

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

Better sorting algorithms (Weiss chapter )

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

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

Quick Sort. CSE Data Structures May 15, 2002

Unit-2 Divide and conquer 2016

Quicksort (Weiss chapter 8.6)

CS Divide and Conquer

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

Parallel Sorting Algorithms

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Sorting Algorithms. + Analysis of the Sorting Algorithms

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

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

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

Cpt S 122 Data Structures. Sorting

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

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

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

CS61BL. Lecture 5: Graphs Sorting

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

CS301 - Data Structures Glossary By

Lecture 8: Mergesort / Quicksort Steven Skiena

COMP Data Structures

BM267 - Introduction to Data Structures

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CSC Design and Analysis of Algorithms

Copyright 2009, Artur Czumaj 1

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

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Divide-and-Conquer. Dr. Yingwu Zhu

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

SORTING. Comparison of Quadratic Sorts

Divide and Conquer. Algorithm D-and-C(n: input size)

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

Reading for this lecture (Goodrich and Tamassia):

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

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

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

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Lecture 6: Divide-and-Conquer

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

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

Chapter 7 Sorting. Terminology. Selection Sort

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

Randomized Algorithms, Quicksort and Randomized Selection

L14 Quicksort and Performance Optimization

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

Divide and Conquer 4-0

Divide and Conquer Sorting Algorithms and Noncomparison-based

Transcription:

Sorting Put array A of n numbers in increasing order. A core algorithm with many applications. Simple algorithms are O(n 2 ). Optimal algorithms are O(n log n). We will see O(n) for restricted input in lab 3. 2004 Goodrich, Tamassia Sorting 1

Insertion Sort 1) Scan A from left to right with index i. 2) Place A[i] into A[1..i] via swaps. 3) Running time Example: 1 4 3 2 O(n 2 ) i = 2: 1 4 3 2 (zero swaps) i = 3: 1 3 4 2 (one swap) i = 4: 1 2 3 4 (two swaps) 2004 Goodrich, Tamassia Sorting 2

Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm. x n Divide: pick a random element x (called pivot) and partition S into L x w L elements less than x, w E elements equal x, and E G w G elements greater than x. n Recur: sort L and G. n Conquer: join L, E and G. x 2004 Goodrich, Tamassia Quick-Sort 3

Partition Algorithm 1) Create arrays L, E, and G. 2) Add each y in S to: L if y < p. E if y = p. G if y > p. This takes O(n) time. 2004 Goodrich, Tamassia Quick-Sort 4

Quick-Sort Tree An execution of quick-sort is depicted by a binary tree. n n Each node represents a recursive call of quick-sort and stores w unsorted sequence before the execution and its pivot, and w sorted sequence at the end of the execution. The root is the initial call. n 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 2004 Goodrich, Tamassia Quick-Sort 5

Execution Example Pivot selection 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 2 2 9 4 4 9 3 3 8 8 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 6

Execution Example (cont.) Partition, recursive call, pivot selection 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 2 4 3 1 2 4 7 9 3 8 6 1 1 3 8 6 2 2 9 4 4 9 3 3 8 8 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 7

Execution Example (cont.) Partition, recursive call, base case 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 2 4 3 1 2 4 7 3 8 6 1 1 3 8 6 1 1 9 4 4 9 3 3 8 8 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 8

Execution Example (cont.) Recursive call,, base case, join 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 2 4 3 1 1 2 3 4 3 8 6 1 1 3 8 6 1 1 4 3 3 4 3 3 8 8 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 9

Execution Example (cont.) Recursive call, pivot selection 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 2 4 3 1 1 2 3 4 7 9 7 1 1 3 8 6 1 1 4 3 3 4 8 8 9 9 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 10

Execution Example (cont.) Partition,, recursive call, base case 7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9 2 4 3 1 1 2 3 4 7 9 7 1 1 3 8 6 1 1 4 3 3 4 8 8 9 9 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 11

Execution Example (cont.) Join, join 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 2 4 3 1 1 2 3 4 7 9 7 17 7 9 1 1 4 3 3 4 8 8 9 9 9 9 4 4 2004 Goodrich, Tamassia Quick-Sort 12

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 1 n - 1 n - 1 1 2004 Goodrich, Tamassia Quick-Sort 13

Expected Running Time Consider a recursive call of quick-sort on a sequence of size s n Good call: the sizes of L and G are each less than 3s/4 n Bad call: one of L and G has size greater than 3s/4 7 2 9 4 3 7 6 1 9 7 2 9 4 3 7 6 1 2 4 3 1 7 9 7 1 1 1 7 2 9 4 3 7 6 Good call Bad call A call is good with probability 1/2 n 1/2 of the possible pivots cause good calls: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bad pivots Good pivots Bad pivots 2004 Goodrich, Tamassia Quick-Sort 14

Expected Running Time, Part 2 Probabilistic Fact: The expected number of coin tosses required in order to get k heads is 2k. For a node of depth i, we expect: n i/2 ancestors are good calls. n The size of the input sequence for the current call is at most (3/4) i/2 n. Therefore, we have n For a node of depth 2log 4/3 n, the expected input size is one. n The expected height of the quick-sort tree is O(log n). The time spent at the nodes of the same depth is O(n). Thus, the expected running time of quick-sort is O(n log n). expected height O(log n) 2004 Goodrich, Tamassia Quick-Sort 15 s(a) s(r) s(b) s(c) s(d) s(e) s(f) time per level O(n) O(n) O(n) total expected time: O(n log n)

In-Place Quick-Sort Quick-sort can be implemented to run in-place. In the partition step, use replace operations to rearrange the elements of the input sequence such that: n the elements less than the pivot have rank less than h, n the elements equal to the pivot have rank between h and k, n the elements greater than the pivot have rank greater than k. The recursive calls consider n elements with rank less than h, n elements with rank greater than k. Algorithm inplacequicksort(s, l, r) Input sequence S, ranks l and r Output sequence S with the elements of rank between l and r rearranged in increasing order if l r return i a random integer between l and r x S.elemAtRank(i) (h, k) inplacepartition(x) inplacequicksort(s, l, h - 1) inplacequicksort(s, k + 1, r) 2004 Goodrich, Tamassia Quick-Sort 16

In-Place Partitioning Perform the partition using two indices to split S into L and E U G (similar method splits 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 Repeat until j and k cross: n Scan j to the right until finding an element > x. n Scan k to the left until finding an element < x. n Swap elements at indices j and k. (pivot = 6) j k 3 2 5 1 0 7 3 5 9 2 7 9 8 9 7 6 9 2004 Goodrich, Tamassia Quick-Sort 17

Merge-Sort Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S 1 and S 2 of about n/ 2 elements each. Recur: recursively sort S 1 and S 2 Conquer: merge S 1 and S 2 into a unique sorted sequence. Algorithm mergesort(s) Input sequence S of size n Output sequence S sorted if n > 1 (S 1, S 2 ) partition(s, n/2) mergesort(s 1, C) mergesort(s 2, C) S merge(s 1, S 2 ) 2004 Goodrich, Tamassia Merge Sort 18

Merging Two Sorted Sequences l l The merge step of merge-sort merges two sorted sequences A and B into a single sorted sequence S. The running time for two sequences with a total of n elements is O(n) using doubly linked lists. Algorithm merge(a, B) Input sequences A and B with n/2 elements each Output sorted sequence of A B S empty sequence while A.empty() B.empty() if A.front() < B.front() S.addBack(A.front()); A.eraseFront(); else S.addBack(B.front()); B.eraseFront(); while A.empty() S.addBack(A.front()); A.eraseFront(); while B.empty() S.addBack(B.front()); B.eraseFront(); return S 2004 Goodrich, Tamassia Merge Sort 19

Merge-Sort Tree An execution of merge-sort is depicted by a binary tree. Each node represents a recursive call of merge-sort and stores: unsorted sequence before the execution and its partition, and 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 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Merge Sort 20

Execution Example Partition 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 21

Execution Example (cont.) Recursive call, partition 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 22

Execution Example (cont.) Recursive call, partition 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 23

Execution Example (cont.) Recursive call, base case 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 24

Execution Example (cont.) Recursive call, base case 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 25

Execution Example (cont.) Merge 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 26

Execution Example (cont.) Recursive call,, base case, merge 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 27

Execution Example (cont.) Merge 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 28

Execution Example (cont.) Recursive call,, merge, merge 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 6 8 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 29

Execution Example (cont.) Merge 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 6 8 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 2004 Goodrich, Tamassia Merge Sort 30

Analysis of Merge-Sort l The height h of the merge-sort tree is O(log n) because each recursive call divides the sequence in half. The total time at the nodes of depth i is O(n) because we partition and merge 2 i sequences of size n/2 i. l The total running time of merge-sort is O(n log n). depth #seqs size 0 1 n 1 2 n/2 i 2 i n/2 i 2004 Goodrich, Tamassia Merge Sort 31

Summary of Sorting Algorithms Algorithm Time Notes selection-sort insertion-sort O(n 2 ) O(n 2 ) in-place slow (good for small inputs) in-place slow (good for small inputs) quick-sort O(n log n) expected in-place, randomized fastest (good for large inputs) heap-sort O(n log n) in-place fast (good for large inputs) merge-sort O(n log n) sequential data access fast (good for huge inputs) 2004 Goodrich, Tamassia Quick-Sort 32