Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/15/20151 Analysis of Algorithms Lecture Analysis of quick sort.

Similar presentations


Presentation on theme: "1/15/20151 Analysis of Algorithms Lecture Analysis of quick sort."— Presentation transcript:

1 1/15/20151 Analysis of Algorithms Lecture Analysis of quick sort

2 1/15/20152 Quick sort Another divide and conquer sorting algorithm – like merge sort Anyone remember the basic idea? The worst-case and average-case running time? Learn some new algorithm analysis tricks

3 1/15/20153 Quick sort Quicksort an n-element array: 1.Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray  x  elements in upper subarray. 2.Conquer: Recursively sort the two subarrays. 3.Combine: Trivial.  x x  x x x x ≥ x≥ x ≥ x≥ x Key: Linear-time partitioning subroutine.

4 1/15/20154 Partition All the action takes place in the partition() function –Rearranges the subarray in place –End result: two subarrays All values in first subarray  all values in second –Returns the index of the “pivot” element separating the two subarrays  x x  x x x x ≥ x≥ x ≥ x≥ x pr q

5 1/15/20155 Pseudocode for quicksort Q UICKSORT (A, p, r) if p < r then q  P ARTITION (A, p, r) Q UICKSORT (A, p, q–1) Q UICKSORT (A, q+1, r) Initial call: Q UICKSORT (A, 1, n)

6 1/15/20156 Idea of partition If we are allowed to use a second array, it would be easy

7 1/15/20157 Another idea Keep two iterators: one from head, one from tail

8 1/15/20158 In-place Partition

9 1/15/20159 Partition In Words Partition(A, p, r): –Select an element to act as the “pivot” (which?) –Grow two regions, A[p..i] and A[j..r] All elements in A[p..i] <= pivot All elements in A[j..r] >= pivot –Increment i until A[i] > pivot –Decrement j until A[j] < pivot –Swap A[i] and A[j] –Repeat until i >= j –Swap A[j] and A[p] –Return j Note: different from book’s partition(), which uses two iterators that both move forward.

10 1/15/ Partition Code Partition(A, p, r) x = A[p];// pivot is the first element i = p; j = r + 1; while (TRUE) { repeat i++; until A[i] > x or i >= j; repeat j--; until A[j] < x or j < i; if (i < j) Swap (A[i], A[j]); else break; } swap (A[p], A[j]); return j; What is the running time of partition() ? partition() runs in  (n) time

11 1/15/ ij x = 6 pr ij ij ij ij ij qpr scan swap final swap Partition example

12 1/15/ Quick sort example

13 1/15/ Analysis of quicksort Assume all input elements are distinct. In practice, there are better partitioning algorithms for when duplicate input elements may exist. Let T(n) = worst-case running time on an array of n elements.

14 1/15/ Worst-case of quicksort Input sorted or reverse sorted. Partition around min or max element. One side of partition always has no elements. (arithmetic series)

15 1/15/ Worst-case recursion tree T(n) = T(0) + T(n–1) + n

16 1/15/ Worst-case recursion tree T(n) = T(0) + T(n–1) + n T(n)T(n)

17 1/15/ n T(0)T(n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n

18 1/15/ n T(0)(n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n T(0)T(n–2)

19 1/15/ n T(0)(n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n T(0)(n–2) T(0)

20 1/15/ n T(0)(n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n T(0)(n–2) T(0) height height = n

21 1/15/ n T(0)(n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n T(0)(n–2) T(0) n height = n

22 1/15/ n (n–1) Worst-case recursion tree T(n) = T(0) + T(n–1) + n (n–2)  (1) n height = n  (1) T(n)=  (n) +  (n 2 ) =  (n 2 )

23 1/15/ Best-case analysis (For intuition only!) If we’re lucky, P ARTITION splits the array evenly: T(n)= 2T(n/2) +  (n) =  (n log n) (same as merge sort) What if the split is always? What is the solution to this recurrence?

24 1/15/ Analysis of “almost-best” case

25 1/15/ Analysis of “almost-best” case n

26 1/15/ Analysis of “almost-best” case n

27 1/15/ Analysis of “almost-best” case  (1) … … log 10/9 n … O(n) leaves n

28 1/15/ log 10 n Analysis of “almost-best” case  (1) … … log 10/9 n T(n)  n log 10/9 n +  (n) … n log 10 n  O(n) leaves  (n log n)

29 1/15/ Quicksort Runtimes Best-case runtime T best (n)   (n log n) Worst-case runtime T worst (n)   (n 2 ) Worse than mergesort? Why is it called quicksort then? Its average runtime T avg (n)   (n log n ) Better even, the expected runtime of randomized quicksort is  (n log n)

30 1/15/ Randomized quicksort Randomly choose an element as pivot –Every time need to do a partition, throw a die to decide which element to use as the pivot –Each element has 1/n probability to be selected Rand-Partition(A, p, r) d = random(); // a random number between 0 and 1 index = p + floor((r-p+1) * d); // p<=index<=r swap(A[p], A[index]); Partition(A, p, r); // now do partition using A[p] as pivot

31 1/15/ Running time of randomized quicksort T(n) = T(0) + T(n–1) + dnif 0 : n–1 split, T(1) + T(n–2) + dnif 1 : n–2 split,  T(n–1) + T(0) + dnif n–1 : 0 split,


Download ppt "1/15/20151 Analysis of Algorithms Lecture Analysis of quick sort."

Similar presentations


Ads by Google