Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7"— Presentation transcript:

1 Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7

2 Quick Sort QuickSort is one of the more widely used sorting methods because: It is not difficult to implement. It works well in a variety of situations. In many situations it consumes fewer resources. It is very well understood and analyzed.

3 Pseudocode for QuickSort
Quick-Sort(A) QSort(A, 1, length[A]) QSort(A, lo, hi) if lo < hi then p <- Partition(A, lo, hi) QSort(A, lo, p) QSort(A, p + 1, hi) Partition(A, lo, hi) {Rearrange A into non-empty segments A[lo..p] and A[p+1..hi] such that all elements in the left segment are less than all elements in the right one. Return partitioning index p.}

4 Two Finger Partition Choose A[lo] as the pivot.
Sort all elements so that elements with value < pivot are on the left and all elements with value > pivot are on the right. Idea: Move two fingers in from the ends of the array until you find something with value <= pivot on the right and something >= pivot on the left. These two values are out of order (if left position < right position), so we swap them. Continue until left >= right. Return partition index (right).

5 Example

6 2 finger partition pseudocode
Two-Finger-Partition(A, lo, hi) pivot <- A[lo] { Choose A[lo] to be pivot } left <- lo { Initially two subarrays } right <- hi { are empty } while true do {Loop invariant at this point: (1) A[lo..left] contains elements <= pivot. (2) A[right..hi] contains elements >= pivot. (3) A[left+1..right-1] hasn't been processed yet.} repeat right <- right { Scan from right end of } until lesseq(A[right], pivot) { array until finding an } { element lesseq than pivot } repeat left <- left { Scan from left end of } until lesseq(pivot, A[left]) { array until finding an } { element greater than pivot } if left < right then { Exchange left and right } swap(A, left, right) else return right { Loop repeats until left >= right} { Right is returned at end }

7 Analysis of QuickSort The running time of quicksort depends on the partitioning algorithm. An important consideration is whether the partition is balanced or unbalanced. Worst case: Partition ends up with 1 subarray of size n-1 and the other subarray of size 1. This is unbalanced partitioning.

8 Running time for unbalanced partition
T(n) = T(n-1) cost to partition cost of one subarray cost of other subarray Cost of 2 finger partition: T2(n) = ? T(n) = ? When does the worst case occur?

9 Best Case Partitioning--Balanced
Balanced partition is when the array is divided in half at each step. T(n) = ?

10 Another uneven split Suppose every split gives 2 arrays whose ratio in size is 99:1 T(n) = T(99n/100) + T(n/100) + n cost of partition We will work this out in class.

11 Alternating "good" and "bad" partitions
It is unlikely to have the same split at every level for a randomly ordered array. What happens if "good" splits alternate with bad splits? n Cost of 2 levels = ? 1 n-1 (n-1)/2 (n-1)/2 Number of levels < 2lgn (Why?) Running time = ?


Download ppt "Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7"

Similar presentations


Ads by Google