Quicksort Z Merge Sort Z T(N) = Θ(N Lg(N)) Z Not In-Place CSE 680 Z Selection Sort (From Homework) Prof
Total Page:16
File Type:pdf, Size:1020Kb
Sorting Review z Insertion Sort Introduction to Algorithms z T(n) = Θ(n2) z In-place Quicksort z Merge Sort z T(n) = Θ(n lg(n)) z Not in-place CSE 680 z Selection Sort (from homework) Prof. Roger Crawfis z T(n) = Θ(n2) z In-place Seems pretty good. z Heap Sort Can we do better? z T(n) = Θ(n lg(n)) z In-place Sorting Comppgarison Sorting z Assumptions z Given a set of n values, there can be n! 1. No knowledge of the keys or numbers we permutations of these values. are sorting on. z So if we look at the behavior of the 2. Each key supports a comparison interface sorting algorithm over all possible n! or operator. inputs we can determine the worst-case 3. Sorting entire records, as opposed to complexity of the algorithm. numbers,,p is an implementation detail. 4. Each key is unique (just for convenience). Comparison Sorting Decision Tree Decision Tree Model z Decision tree model ≤ 1:2 > z Full binary tree 2:3 1:3 z A full binary tree (sometimes proper binary tree or 2- ≤≤> > tree) is a tree in which every node other than the leaves has two c hildren <1,2,3> ≤ 1:3 >><2,1,3> ≤ 2:3 z Internal node represents a comparison. z Ignore control, movement, and all other operations, just <1,3,2> <3,1,2> <2,3,1> <3,2,1> see comparison z Each leaf represents one possible result (a permutation of the elements in sorted order). Internal node i:j indicates comparison between ai and aj. z The height of the tree (i. e., longest path) is the suppose three elements < a1, a2, a3> with instance <6, 8, 5> lower bound. Leaf node <π(1), π(2), π(3)> indicates ordering aπ(1)≤ aπ(2)≤ aπ(3). Path of bold lines indicates sorting path for <6,8,5>. There are total 3!=6 possible permutations (paths). Decision Tree Model QuickSort Design z The longest path is the worst case number of z Follows the divide-and-conquer paradigm. comparisons. The length of the longest path is the z Divide: Partition (separate) the array A[p..r] into two height of the decision tree. (possibly empty) subarrays A[p..q–1] and A[q+1..r]. z Theorem 8.1: Anyyp comparison sort alg orithm z Each element in A[p..q–1] < A[q]. requires Ω(nlg n) comparisons in the worst case. z A[q] < each element in A[q+1..r]. z Index q is computed as part of the partitioning procedure. z Proof: z Conquer: Sort the two subarrays by recursive calls to z Suppose height of a decision tree is h, and number of quikicksor t. paths (i,e,, permutations) is n!. z Since a binary tree of height h has at most 2h leaves, z Combine: The subarrays are sorted in place – no z n! ≤ 2h , so h ≥ lg (n!) ≥Ω(nlg n))( (By eq uation 3.18) . workwork iiss nneededeeded to cocombinembine tthem.hem. z That is to say: any comparison sort in the worst z How do the divide and combine steps of quicksort case needs at least nlg n comparisons. compare with those of merge sort? Pseudocode Example p r Quicksort(A, p, r) initially: 2 5 8 3 9 4 1 7 10 6 note: pivot (x) = 6 if p<rp < r then Ptiti(APartition(A, p, r ) ij q := Partition(A, p, r); x, i := A[r], p – 1; Quicksort(A, p, q – 1); for j := p to r – 1 do next iteration: 2 5 8 3 9 4 1 7 10 6 Quicksort(A, q + 1, r) if A[j] ≤ x then i j Partition(A, p, r) i:= i+ 1; x, i := A[r], p – 1; A[p..r] A[i] ↔ A[j] next iteration: 258 3 9 4 1 7 10 6 for j := p to r – 1 do A[i +1]+ 1] ↔ A[r]; i j if A[j] ≤ x then 5 return i+ 1 i:= i+ 1; next iteration: 2 5 8 3 9 4 1 7 10 6 A[i] ↔ A[j] A[p..q – 1] A[q+1..r] ij A[i+ 1] ↔ A[[];r]; Partition return i+ 1 5 next iteration: 2 5 3 8 9 4 1 7 10 6 ij ≤ 5 ≥ 5 Exampp(le (Continued) Partitioning next iteration: 2 5 3 8 9 4 1 7 10 6 z Select the last element A[r] in the subarray i j A[p..r] as the pivot – the element around which next iteration: 2 5 3 8 9 4 1 7 10 6 to partition. ij next iteration: 252 5 3 4 989 8 17101 7 10 6 Partition(A, p, r) z As the pp,yrocedure executes, the array is ij x, i := A[r], p – 1; partitioned into four (possibly empty) regions. next iteration: 2 5 3 4 1 8 9 7 10 6 for j := p to r – 1 do 1. A[p..i ] — All entries in this region are < pivot. i j if A[j] ≤ x then 2. A[i+1..j – 1] — All entries in this region are > pivot. next iteration: 2 5 3 4 1 8 9 7 10 6 i:= i+ 1; 3. A[r] = pivot. ij A[i] ↔ A[j] 4. A[j..r – 1] — Not known how they compare to pivot. next iteration: 2 5 3 4 1 8 9 7 10 6 A[i+ 1] ↔ A[[]r]; z The above hold before each iteration of the for ijreturn i+ 1 loop, and constitute a loop invariant. (4 is not part after final swap: 2 53416 9 7 10 8 of the loopi.) ij Correctness of Partition Correctness of Partition z Use loop invariant. Case 1: z In itia lizati on: p i j r z Before first iteration >x x z A[p..i] and A[i+1..j – 1] are empty – Conds. 1 and 2 are satisfied (trivially). z r is the index of the pivot Partition(A, p, r) ≤ x > x z Cond. 3 is satisfied. x, i := A[r], p – 1; p i j r for j := p to r – 1 do x z Maintenance: if A[j] ≤ x then z Case 1: A[j] > x i:= i+ 1; A[i] ↔ A[j] z Increment j only. ≤ x > x A[i + 1] ↔ A[r]; z Loop Invariant is maintained. return i+ 1 Correctness of Partition Correctness of Partition z Case 2: A[j] ≤ x z Increment j z Termination: z Condition 2 is z Increment i z When the loop terminates, j = r, so all elements maintained. z Swap A[i] and A[j] in A are partitioned into one of the three cases: z A[r] is unaltered. z Condition 1 is z A[p..i] ≤ pivot z Condition 3 is maintained. z A[i+1..j – 1] > pivot maintained. p i j r z A[r] = pivot ≤ x x z The last two lines swap A[i+1] and A[r]. z Pivot moves from the end of the array to ≤ x > x between the two subarrays. p i j r z Thus, procedure partition correctly performs x the divide step. ≤ x > x Comppylexity of Partition Quicksort Overview z PartitionTime(n) is given by the number z TtTo sort a[le ft...r ig ht]: of iterations in the for loop. 1. if left < right: z Θ(n) : n = r – p +1+ 1. 111.1. Par tition a[le ft...r ight] such ththtat: Partition(A, p, r) x, i := A[r], p – 1; all a[left...p-1] are less than a[p], and for j:j := p to r – 1 do if A[j] ≤ x then all a[p+1...right] are >= a[p] i:= i+ 1; 1.2. Quicksort a[left...p-1] A[i] ↔ A[j] A[i + 1] ↔ A[r]; 1.3. Quicksort a[p+1...right] return i+ 1 2. Terminate Partitioning in Quicksort Alternative Partitioning z A key step in the Quicksort algorithm is z Choose an array value ( say, the first) to use partitioning the array as the pivot z Starting from the left end, find the first z We choose some ((y)any) number p in the element that is greater than or equal to the array to use as a pivot pivot z We partition the array into three parts: z Searching backward from the right end, find the first element that is less than the pivot p z Interchange (swap) these two elements z Repeat, searching from where we left off, numbers less p numbers greater than or until done than p equal to p Alternative Partitioning Examppple of partitionin g z choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 z To par tition a[l e ft...ri g ht]: z search: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 1. Set pivot = a[left], l = left + 1, r = right; z swap: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6 2. while l < r, do z search: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6 2.1. while l < right & a[l] < pivot , set l = l + 1 z swap: 4 331 2 4 3 1 2 9 8 9 6 5 6 222.2. while r > lftleft & a[][r] >= pivo t , set r = r - 1 z search: 4 33 1 24 3 1 2 9 8 9 6 5 6 2.3.