Download presentation
Presentation is loading. Please wait.
Published byDinah Stephens Modified over 7 years ago
1
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Jory Denny and Mukulika Ghosh 1 1
2
Divide and Conquer Algorithms analysis with recurrence equations
Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data ๐ into ๐ (disjoint) subsets ๐ 1 , ๐ 2 ,โฆ, ๐ ๐ Recur: solve the sub-problems recursively Conquer: combine the solutions for ๐ 1 , ๐ 2 ,โฆ, ๐ ๐ into a solution for ๐ The base case for the recursion are sub-problems of constant size Analysis can be done using recurrence equations (relations)
3
Divide and Conquer Algorithms analysis with recurrence equations
When the size of all sub-problems is the same (frequently the case) the recurrence equation representing the algorithm is: ๐ ๐ =๐ท ๐ +๐๐ ๐ ๐ +๐ถ ๐ Where ๐ท ๐ is the cost of dividing ๐ into the ๐ sub-problems ๐ 1 , ๐ 2 ,โฆ, ๐ ๐ There are ๐ sub-problems, each of size ๐ ๐ that will be solved recursively ๐ถ ๐ is the cost of combining the sub-problem solutions to get the solution for ๐
4
Exercise Recurrence Equation Setup
Algorithm โ transform multiplication of two ๐-bit integers ๐ผ and ๐ฝ into multiplication of ๐ 2 -bit integers and some additions/shifts Where does recursion happen in this algorithm? Rewrite the step(s) of the algorithm to show this clearly. Algorithm multiply ๐ผ,๐ฝ Input: ๐-bit integers ๐ผ,๐ฝ Output: ๐ผโ๐ฝ if ๐>1 Split ๐ผ and ๐ฝ into high and low order halves: ๐ผ โ , ๐ผ ๐ , ๐ฝ โ , ๐ฝ ๐ ๐ฅ 1 โ ๐ผ โ โ ๐ฝ โ ; ๐ฅ 2 โ ๐ผ โ โ ๐ฝ ๐ ; ๐ฅ 3 โ ๐ผ ๐ โ ๐ฝ โ ; ๐ฅ 4 โ ๐ผ ๐ โ ๐ฝ ๐ ๐โ ๐ฅ 1 โ 2 ๐ + ๐ฅ 2 โ 2 ๐ 2 + ๐ฅ 3 โ 2 ๐ 2 + ๐ฅ 4 else ๐โ๐ผโ๐ฝ return ๐
5
Exercise Recurrence Equation Setup
Algorithm โ transform multiplication of two ๐-bit integers ๐ผ and ๐ฝ into multiplication of ๐ 2 -bit integers and some additions/shifts Assuming that additions and shifts of ๐-bit numbers can be done in ๐ ๐ time, describe a recurrence equation showing the running time of this multiplication algorithm Algorithm multiply ๐ผ,๐ฝ Input: ๐-bit integers ๐ผ,๐ฝ Output: ๐ผโ๐ฝ if ๐>1 Split ๐ผ and ๐ฝ into high and low order halves: ๐ผ โ , ๐ผ ๐ , ๐ฝ โ , ๐ฝ ๐ ๐ฅ 1 โmultiply ๐ผ โ , ๐ฝ โ ; ๐ฅ 2 โmultiply ๐ผ โ , ๐ฝ ๐ ; ๐ฅ 3 โmultiply ๐ผ ๐ , ๐ฝ โ ; ๐ฅ 4 โmultiply ๐ผ ๐ , ๐ฝ ๐ ๐โ ๐ฅ 1 โ 2 ๐ + ๐ฅ 2 โ 2 ๐ 2 + ๐ฅ 3 โ 2 ๐ 2 + ๐ฅ 4 else ๐โ๐ผโ๐ฝ return ๐
6
Exercise Recurrence Equation Setup
Algorithm โ transform multiplication of two ๐-bit integers ๐ผ and ๐ฝ into multiplication of ๐ 2 -bit integers and some additions/shifts The recurrence equation for this algorithm is: ๐ ๐ =4๐ ๐ 2 +๐ ๐ The solution is ๐ ๐ 2 which is the same as naรฏve algorithm Algorithm multiply ๐ผ,๐ฝ Input: ๐-bit integers ๐ผ,๐ฝ Output: ๐ผโ๐ฝ if ๐>1 Split ๐ผ and ๐ฝ into high and low order halves: ๐ผ โ , ๐ผ ๐ , ๐ฝ โ , ๐ฝ ๐ ๐ฅ 1 โmultiply ๐ผ โ , ๐ฝ โ ; ๐ฅ 2 โmultiply ๐ผ โ , ๐ฝ ๐ ; ๐ฅ 3 โmultiply ๐ผ ๐ , ๐ฝ โ ; ๐ฅ 4 โmultiply ๐ผ ๐ , ๐ฝ ๐ ๐โ ๐ฅ 1 โ 2 ๐ + ๐ฅ 2 โ 2 ๐ 2 + ๐ฅ 3 โ 2 ๐ 2 + ๐ฅ 4 else ๐โ๐ผโ๐ฝ return ๐
7
Merge Sort Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ 7 2 ๏ฝ 9 4 ๏ฎ 2 4 7 9 7 ๏ฝ 2 ๏ฎ 2 7
7 2 ๏ฝ ๏ฎ 7 ๏ฝ 2 ๏ฎ 2 7 9 ๏ฝ 4 ๏ฎ 4 9 7 ๏ฎ 7 2 ๏ฎ 2 9 ๏ฎ 9 4 ๏ฎ 4 Merge Sort
8
Merge-Sort Merge-sort is based on the divide-and-conquer paradigm. It consists of three steps: Divide: partition input sequence ๐ into two sequences ๐ 1 and ๐ 2 of about ๐ 2 elements each Recur: recursively sort ๐ 1 and ๐ 2 Conquer: merge ๐ 1 and ๐ 2 into a sorted sequence Algorithm mergeSort(๐, ๐ถ) Input: Sequence ๐ of ๐ elements, Comparator ๐ถ Output: Sequence ๐ sorted according to ๐ถ if ๐.๐ ๐๐ง๐ >1 ๐ 1 , ๐ 2 โpartition ๐, ๐ 2 ๐ 1 โmergeSort ๐ 1 , ๐ถ ๐ 2 โmergeSort ๐ 2 ,๐ถ ๐โmerge ๐ 1 , ๐ 2 return ๐
9
Merge-Sort Execution Tree (recursive calls)
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 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 ๏ฝ ๏ฎ 7 ๏ฝ 2 ๏ฎ 2 7 9 ๏ฝ 4 ๏ฎ 4 9 7 ๏ฎ 7 2 ๏ฎ 2 9 ๏ฎ 9 4 ๏ฎ 4
10
Execution Example Partition 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 ๏ฝ ๏ฎ
11
Execution Example Recursive Call, partition 7 2 | 9 4 ๏ฎ 2 4 7 9
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
12
Execution Example Recursive Call, partition 7 2 | 9 4 ๏ฎ 2 4 7 9
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
13
Execution Example Recursive Call, base case 7 2 | 9 4 ๏ฎ 2 4 7 9
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
14
Execution Example Recursive Call, base case 7 2 | 9 4 ๏ฎ 2 4 7 9
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
15
Execution Example Merge 7 2 | 9 4 ๏ฎ 2 4 7 9 3 8 6 1 ๏ฎ 1 3 8 6
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
16
Execution Example Recursive call, โฆ, base case, merge
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
17
Execution Example Merge 7 2 | 9 4 ๏ฎ 2 4 7 9 3 8 6 1 ๏ฎ 1 3 8 6
7 2 | ๏ฎ ๏ฎ 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 ๏ฝ ๏ฎ
18
Execution Example Recursive call, โฆ, merge, merge 7 2 | 9 4 ๏ฎ 2 4 7 9
7 2 | ๏ฎ 3 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 ๏ฝ ๏ฎ
19
Execution Example Merge 7 2 | 9 4 ๏ฎ 2 4 7 9 3 8 | 6 1 ๏ฎ 1 3 8 6
7 2 | ๏ฎ 3 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 ๏ฝ ๏ฎ
20
Analysis Using Recurrence relation
The running time of Merge Sort can be expressed by the recurrence equation: ๐ ๐ =2๐ ๐ 2 +๐ ๐ We need to determine ๐ ๐ , the time to merge two sorted sequences each of size ๐ 2 . Algorithm mergeSort(๐, ๐ถ) Input: Sequence ๐ of ๐ elements, Comparator ๐ถ Output: Sequence ๐ sorted according to ๐ถ if ๐.๐ ๐๐ง๐ >1 ๐ 1 , ๐ 2 โpartition ๐, ๐ 2 ๐ 1 โmergeSort ๐ 1 , ๐ถ ๐ 2 โmergeSort ๐ 2 ,๐ถ ๐โmerge ๐ 1 , ๐ 2 return ๐
21
Merging Two Sorted Sequences
The conquer step of merge-sort consists of merging two sorted sequences ๐ด and ๐ต into a sorted sequence ๐ containing the union of the elements of ๐ด and ๐ต Merging two sorted sequences, each with ๐ 2 elements and implemented by means of a doubly linked list, takes ๐ ๐ time ๐ ๐ =๐ ๐ Algorithm ๐๐๐๐๐(๐ด, ๐ต) Input: Sequences ๐ด,๐ต with ๐ 2 elements each Output: Sorted sequence of ๐ดโช๐ต ๐โโ
while ยฌ๐ด.๐๐๐๐ก๐ฆ โงยฌ๐ต.๐๐๐๐ก๐ฆ if ๐ด.๐๐๐๐๐ก <๐ต.๐๐๐๐๐ก ๐.๐๐๐ ๐๐๐ก๐ต๐๐๐ ๐ด.๐๐๐๐๐ก ; ๐ด.๐๐๐๐ ๐๐น๐๐๐๐ก else ๐.๐๐๐ ๐๐๐ก๐ต๐๐๐ ๐ต.๐๐๐๐๐ก ; ๐ต.๐๐๐๐ ๐๐น๐๐๐๐ก while ยฌ๐ด.๐๐๐๐ก๐ฆ while ยฌ๐ต.๐๐๐๐ก๐ฆ return ๐
22
And the complexity of mergesortโฆ
Algorithm mergeSort(๐, ๐ถ) Input: Sequence ๐ of ๐ elements, Comparator ๐ถ Output: Sequence ๐ sorted according to ๐ถ if ๐.๐ ๐๐ง๐ >1 ๐ 1 , ๐ 2 โpartition ๐, ๐ 2 ๐ 1 โmergeSort ๐ 1 , ๐ถ ๐ 2 โmergeSort ๐ 2 ,๐ถ ๐โmerge ๐ 1 , ๐ 2 return ๐ So, the running time of Merge Sort can be expressed by the recurrence equation: ๐ ๐ =2๐ ๐ 2 +๐ ๐ =2๐ ๐ 2 +๐ ๐ =๐ ๐ log ๐
23
Another Analysis of Merge-Sort
The height โ of the merge-sort tree is ๐ log ๐ at each recursive call we divide in half the sequence, The work done at each level is ๐ ๐ At level ๐, we partition and merge 2 ๐ sequences of size ๐ 2 ๐ Thus, the total running time of merge-sort is ๐ ๐ log ๐ depth #seqs size Cost for level 1 ๐ 2 n/2 โฆ i 2 ๐ ๐ 2 ๐ log ๐ 2 log ๐ =๐ ๐ 2 log ๐ =1
24
Summary of Sorting Algorithms (so far)
Time Notes Selection Sort ๐ ๐ 2 Slow, in-place For small data sets Insertion Sort ๐ ๐ 2 WC, AC ๐ ๐ BC Heap Sort ๐ ๐ log ๐ Fast, in-place For large data sets Merge Sort Fast, sequential data access For huge data sets
25
Quick-Sort Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ 7 4 9 6 2 ๏ฎ 2 4 6 7 9 4 2 ๏ฎ 2 4
๏ฎ 4 2 ๏ฎ 2 4 7 9 ๏ฎ 7 9 2 ๏ฎ 2 9 ๏ฎ 9
26
Quick-Sort x L G E Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element ๐ฅ (called pivot) and partition ๐ into ๐ฟ - elements less than ๐ฅ ๐ธ - elements equal ๐ฅ ๐บ - elements greater than ๐ฅ Recur: sort ๐ฟ and ๐บ Conquer: join ๐ฟ, ๐ธ, and ๐บ
27
Partition Algorithm partition ๐, ๐
We partition an input sequence as follows: We remove, in turn, each element ๐ฆ from ๐ and We insert ๐ฆ into ๐ฟ, ๐ธ, or ๐บ, depending on the result of the comparison with the pivot ๐ฅ Algorithm partition ๐, ๐ Input: Sequence ๐, position ๐ of the pivot Output: Subsequences ๐ฟ, ๐ธ, ๐บ of the elements of ๐ less than, equal to, or greater than the pivot, respectively ๐ฟ, ๐ธ, ๐บโโ
๐ฅโ๐.erase ๐ while ยฌ๐.empty ๐ฆโ๐.eraseFront if ๐ฆ<๐ฅ ๐ฟ.insertBack ๐ฆ else if ๐ฆ=๐ฅ ๐ธ.insertBack ๐ฆ else //๐ฆ>๐ฅ ๐บ.insertBack ๐ฆ return ๐ฟ,๐ธ,๐บ
28
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 ๏ฎ 4 2 ๏ฎ 2 4 7 9 ๏ฎ 7 9 2 ๏ฎ 2 9 ๏ฎ 9
29
Execution Example Pivot selection 2 4 3 1 ๏ฎ 1 2 3 4 1 ๏ฎ 1
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
30
Execution Example Partition, recursive call, pivot selection
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
31
Execution Example Partition, recursive call, base case
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
32
Execution Example Recursive call, โฆ, base case, join 2 4 3 1 ๏ฎ 1 2 3 4
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
33
Execution Example Recursive call, pivot selection 2 4 3 1 ๏ฎ 1 2 3 4
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
34
Execution Example Partition, โฆ, recursive call, base case
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
35
Execution Example Join, join 2 4 3 1 ๏ฎ 1 2 3 4 1 ๏ฎ 1
๏ฎ 1 ๏ฎ 1 ๏ฎ ๏ฎ 9 ๏ฎ 9 4 3 ๏ฎ 3 4 4 ๏ฎ 4
36
In-Place Quick-Sort Algorithm inPlaceQuickSort(๐, ๐, ๐)
Quick-sort can be implemented to run in-place In the partition step, we use replace operations to rearrange the elements of the input sequence such that the elements less than the pivot have indices less than โ the elements equal to the pivot have indices between โ and ๐ the elements greater than the pivot have indices greater than ๐ The recursive calls consider elements with indices less than โ elements with indices greater than ๐ Algorithm inPlaceQuickSort(๐, ๐, ๐) Input: Array ๐, indices ๐, r Output: Array ๐ with the elements between ๐ and ๐ sorted if ๐โฅ๐ return ๐ ๐โrand % ๐โ๐ +๐ //random integer between ๐ and ๐ ๐ฅโ๐ ๐ โ, ๐ โinPlacePartition ๐ฅ inPlaceQuickSort(๐, ๐, โโ1) inPlaceQuickSort ๐, ๐+1, ๐
37
In-Place Partitioning
Perform the partition using two indices to split ๐ into ๐ฟ and ๐ธโช๐บ (a similar method can split ๐ธโช๐บ into ๐ธ and ๐บ). Repeat until ๐ and ๐ cross: Scan ๐ to the right until finding an element โฅ๐ฅ. Scan ๐ to the left until finding an element <๐ฅ. Swap elements at indices ๐ and ๐ j k (pivot = 6) ๐ ๐
38
Analysis of Quick Sort using Recurrence Relations
Algorithm quickSort(๐, ๐, ๐) Input: Sequence ๐, indices ๐, r Output: Sequence ๐ with the elements between ๐ and ๐ sorted if ๐โฅ๐ return ๐ ๐โrand % ๐โ๐ +๐ //random integer between ๐ and ๐ ๐ฅโ๐.at ๐ โ, ๐ โpartition ๐ฅ quickSort ๐, ๐, โโ1 quickSort ๐, ๐+1 , ๐ Assumption: random pivot expected to give equal sized sub-lists The running time of Quick Sort can be expressed as: ๐ ๐ =2๐ ๐ 2 +๐ ๐ ๐ ๐ - time to run partition on input of size ๐
39
Analysis of Partition Algorithm partition ๐, ๐
Each insertion and removal is at the beginning or at the end of a sequence, and hence takes ๐ 1 time Thus, the partition step of quick-sort takes ๐ ๐ time Algorithm partition ๐, ๐ Input: Sequence ๐, position ๐ of the pivot Output: Subsequences ๐ฟ, ๐ธ, ๐บ of the elements of ๐ less than, equal to, or greater than the pivot, respectively ๐ฟ, ๐ธ, ๐บโโ
๐ฅโ๐.erase ๐ while ยฌ๐.empty ๐ฆโ๐.eraseFront if ๐ฆ<๐ฅ ๐ฟ.insertBack ๐ฆ else if ๐ฆ=๐ฅ ๐ธ.insertBack ๐ฆ else //๐ฆ>๐ฅ ๐บ.insertBack ๐ฆ return ๐ฟ,๐ธ,๐บ
40
So, the expected complexity of Quick Sort
Assumption: random pivot expected to give equal sized sub-lists The running time of Quick Sort can be expressed as: ๐ ๐ =2๐ ๐ 2 +๐ ๐ =2๐ ๐ 2 +๐ ๐ =๐ ๐ log ๐ Algorithm quickSort(๐, ๐, ๐) Input: Sequence ๐, indices ๐, r Output: Sequence ๐ with the elements between ๐ and ๐ sorted if ๐โฅ๐ return ๐ ๐โrand % ๐โ๐ +๐ //random integer between ๐ and ๐ ๐ฅโ๐.at ๐ โ, ๐ โpartition ๐ฅ quickSort ๐, ๐, โโ1 quickSort ๐, ๐+1 , ๐
41
Worst-case Running Time
depth time n 1 n - 1 โฆ The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of ๐ฟ and ๐บ has size ๐โ1 and the other has size 0 The running time is proportional to: ๐+ ๐โ1 +โฆ+2+1=๐ ๐ 2 Alternatively, using recurrence equations: ๐ ๐ =๐ ๐โ1 +๐ ๐ =๐ ๐ 2 โฆ
42
Expected Running Time removing equal split assumption
Consider a recursive call of quick-sort on a sequence of size ๐ Good call: the sizes of ๐ฟ and ๐บ are each less than 3๐ 4 Bad call: one of ๐ฟ and ๐บ has size greater than 3๐ 4 A call is good with probability 1/2 1/2 of the possible pivots cause good calls: 1 Good call Bad call Bad pivots Good pivots Bad pivots
43
Expected Running Time Probabilistic Fact: The expected number of coin tosses required in order to get ๐ heads is 2๐ (e.g., it is expected to take 2 tosses to get heads) For a node of depth ๐, we expect ๐ 2 ancestors are good calls The size of the input sequence for the current call is at most ๐ 2 ๐ Therefore, we have For a node of depth 2 log ๐ , the expected input size is one The expected height of the quick-sort tree is ๐ log ๐ The amount or work done at the nodes of the same depth is ๐ ๐ Thus, the expected running time of quick-sort is ๐ ๐ log ๐
44
Summary of Sorting Algorithms (so far)
Time Notes Selection Sort ๐ ๐ 2 Slow, in-place For small data sets Insertion Sort ๐ ๐ 2 WC, AC ๐ ๐ BC Heap Sort ๐ ๐ log ๐ Fast, in-place For large data sets Quick Sort Exp. ๐ ๐ log ๐ AC, BC ๐ ๐ 2 WC Fastest, randomized, in-place Merge Sort Fast, sequential data access For huge data sets
45
(2,4) Trees 1/12/2018 9:05 AM Sorting Lower Bound
46
Comparison-Based Sorting
Many sorting algorithms are comparison based. They sort by making comparisons between pairs of objects Examples: bubble-sort, selection-sort, insertion-sort, heap-sort, merge-sort, quick-sort, ... Let us therefore derive a lower bound on the running time of any algorithm that uses comparisons to sort ๐ elements, ๐ฅ 1 , ๐ฅ 2 ,โฆ, ๐ฅ ๐ . Is ๐ฅ ๐ < ๐ฅ ๐ ? yes no
47
Counting Comparisons Let us just count comparisons then.
Each possible run of the algorithm corresponds to a root-to-leaf path in a decision tree
48
Decision Tree Height The height of the decision tree is a lower bound on the running time Every input permutation must lead to a separate leaf output If not, some input โฆ4โฆ5โฆ would have same output ordering as โฆ5โฆ4โฆ, which would be wrong Since there are ๐!=1โ2โโฆโ๐ leaves, the height is at least log ๐!
49
The Lower Bound Any comparison-based sorting algorithm takes at least log ๐! time log (๐!) โฅ log ๐ 2 ๐ 2 = ๐ 2 log ๐ 2 That is, any comparison-based sorting algorithm must run in ฮฉ(๐ log ๐ ) time.
50
Bucket-Sort and Radix-Sort
Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ Bucket-Sort and Radix-Sort Can we sort in linear time? 1 2 3 4 5 6 7 8 9 B 1, c 7, d 7, g 3, b 3, a 7, e ๏
51
Bucket-Sort Let be S be a sequence of ๐ (key, element) items with keys in the range 0,๐โ1 Bucket-sort uses the keys as indices into an auxiliary array ๐ต of sequences (buckets) Phase 1: Empty sequence ๐ by moving each entry into its bucket ๐ต ๐ Phase 2: for ๐โ0โฆ๐โ1, move the items of bucket ๐ต ๐ to the end of sequence ๐ Analysis: Phase 1 takes ๐ ๐ time Phase 2 takes ๐ ๐+๐ time Bucket-sort takes ๐ ๐+๐ time Algorithm bucketSort(๐, ๐) Input: Sequence ๐ of entries with integer keys in the range 0, ๐โ1 Output: Sequence ๐ sorted in non-decreasing order of the keys ๐ตโ array of ๐ empty sequences for each entry ๐โ๐ do ๐โ๐.key remove ๐ from ๐ and insert it at the end of bucket ๐ต ๐ for ๐โ0โฆ๐โ1 do for each entry ๐โ๐ต ๐ do remove ๐ from bucket ๐ต ๐ and insert it at the end of ๐
52
Properties and Extensions
Key-type The keys are used as indices into an array and cannot be arbitrary objects No external comparator Stable sorting The relative order of any two items with the same key is preserved after the execution of the algorithm Extensions Integer keys in the range ๐,๐ Put entry ๐ into bucket ๐ต ๐โ๐ String keys from a set ๐ท of possible strings, where ๐ท has constant size (e.g., names of the 50 U.S. states) Sort ๐ท and compute the index ๐ ๐ of each string ๐ of ๐ท in the sorted sequence Put item ๐ into bucket ๐ต ๐ ๐
53
Example Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ 45, d 37, c 40, a 45, g 40, b 46, e
Key range [37, 46] โ map to buckets [0,9] Example 45, d 37, c 40, a 45, g 40, b 46, e Phase 1 1 2 3 4 5 6 7 8 9 B 37, c 45, d 45, g 40, b 40, a 46, e ๏ Is it in-place? Is it stable? Can we use a hash table instead of a bucket array? Phase 2 37, c 40, a 40, b 45, d 45, g 46, e
54
Lexicographic Order Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ
Given a list of tuples: (7,4,6) (5,1,5) (2,4,6) (2,1,4) (5,1,6) (3,2,4) After sorting, the list is in lexicographical order: (2,1,4) (2,4,6) (3,2,4) (5,1,5) (5,1,6) (7,4,6) Non-decreasing order
55
Lexicographic Order Formalized
A ๐-tuple is a sequence of ๐ keys ๐ 1 , ๐ 2 ,โฆ, ๐ ๐ , where key ๐ ๐ is said to be the ๐-th dimension of the tuple Example - the Cartesian coordinates of a point in space is a 3-tuple ๐ฅ, ๐ฆ, ๐ง The lexicographic order of two ๐-tuples is recursively defined as follows ๐ฅ 1 , ๐ฅ 2 ,โฆ, ๐ฅ ๐ < ๐ฆ 1 , ๐ฆ 2 ,โฆ, ๐ฆ ๐ ๐ฅ 1 < ๐ฆ 1 โจ ๐ฅ 1 = ๐ฆ 1 โง ๐ฅ 2 ,โฆ, ๐ฅ ๐ < ๐ฆ 2 ,โฆ, ๐ฆ ๐ i.e., the tuples are compared by the first dimension, then by the second dimension, etc.
56
Exercise Lexicographic Order
Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ Exercise Lexicographic Order Given a list of 2-tuples, we can order the tuples lexicographically by applying a stable sorting algorithm two times: (3,3) (1,5) (2,5) (1,2) (2,3) (1,7) (3,2) (2,2) Possible ways of doing it: Sort first by 1st element of tuple and then by 2nd element of tuple Sort first by 2nd element of tuple and then by 1st element of tuple Show the result of sorting the list using both options Non-decreasing order What must it be stable?
57
Exercise Lexicographic Order
Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ Exercise Lexicographic Order (3,3) (1,5) (2,5) (1,2) (2,3) (1,7) (3,2) (2,2) Using a stable sort, Sort first by 1st element of tuple and then by 2nd element of tuple Sort first by 2nd element of tuple and then by 1st element of tuple Option 1: 1st sort: (1,5) (1,2) (1,7) (2,5) (2,3) (2,2) (3,3) (3,2) 2nd sort: (1,2) (2,2) (3,2) (2,3) (3,3) (1,5) (2,5) (1,7) - WRONG Option 2: 1st sort: (1,2) (3,2) (2,2) (3,3) (2,3) (1,5) (2,5) (1,7) 2nd sort: (1,2) (1,5) (1,7) (2,2) (2,3) (2,5) (3,2) (3,3) - CORRECT Non-decreasing order What must it be stable?
58
Lexicographic-Sort Let ๐ถ ๐ be the comparator that compares two tuples by their ๐-th dimension Let stableSort(๐, ๐ถ) be a stable sorting algorithm that uses comparator ๐ถ Lexicographic-sort sorts a sequence of ๐-tuples in lexicographic order by executing ๐ times algorithm stableSort, one per dimension Lexicographic-sort runs in ๐ ๐๐ ๐ time, where ๐ ๐ is the running time of stableSort Algorithm lexicographicSort(๐) Input: Sequence ๐ of ๐-tuples Output: Sequence ๐ sorted in lexicographic order for ๐โ๐โฆ1 do stableSort ๐, ๐ถ ๐
59
Radix-Sort Radix-sort is a specialization of lexicographic-sort that uses bucket-sort as the stable sorting algorithm in each dimension Radix-sort is applicable to tuples where the keys in each dimension ๐ are integers in the range 0,๐โ1 Radix-sort runs in time ๐ ๐ ๐+๐ Algorithm radixSort ๐,๐ Input: Sequence ๐ of ๐-tuples such that ,โฆ,0 โค ๐ฅ 1 ,โฆ, ๐ฅ ๐ and ๐ฅ 1 ,โฆ, ๐ฅ ๐ โค ๐โ1,โฆ,๐โ for each tuple ๐ฅ 1 ,โฆ, ๐ฅ ๐ in ๐ Output: Sequence ๐ sorted in lexicographic order for ๐โ๐โฆ1 do set the key ๐ of each entry ๐, ๐ฅ 1 ,โฆ, ๐ฅ ๐ of ๐ to ๐th dimension ๐ฅ ๐ bucketSort ๐,๐
60
Example Radix-Sort for Binary Numbers
Merge Sort ไบโไธๅ
ซๅนดไธๆๅไบๆฅ Sorting a sequence of 4-bit integers ๐=4,๐=2 so ๐ ๐ ๐+๐ =๐ 4 ๐+2 =๐ ๐ Example Radix-Sort for Binary Numbers 1001 0010 1101 0001 1110 0010 1110 1001 1101 0001 1001 1101 0001 0010 1110 1001 0001 0010 1101 1110 0001 0010 1001 1101 1110 Whatโs the complexity? Whatโs the constant in O(d( n + N)) Sort by d=4 Sort by d=3 Sort by d=2 Sort by d=1
61
Summary of Sorting Algorithms
Time Notes Selection Sort ๐(๐2) Slow, in-place For small data sets Insertion Sort ๐(๐2) WC, AC ๐(๐) BC Heap Sort ๐(๐ log ๐ ) Fast, in-place For large data sets Quick Sort Exp. ๐ ๐ log ๐ AC, BC ๐(๐2) WC Fastest, randomized, in-place Merge Sort Fast, sequential data access For huge data sets Radix Sort ๐ ๐ ๐+๐ , ๐ #digits, ๐ range of digit values Fastest, stable only for integers
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.