Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II

2 Pseudocode for QuickSort
Quick-Sort(A) QSort(A, 1, length[A]) QSort(A, lo, hi) if lo < hi 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.}

3 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.

4 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 = 2n -1 = Q(n) 1 n-1 (n-1)/2 (n-1)/2 Number of levels < 2lgn (Why?) Running time = O(nlgn)

5 Average case running time of Quick Sort
The growth of the running time for QuickSort when the levels alternate between good and bad splits is like the growth of the running time for good splits alone: O(nlgn) On average, QuickSort has a running time that is O(nlgn). This assumes that we will encounter the various inputs with equal likelihood. Instead of assuming all permutations are equally likely, we can enforce it by permuting the elements before sorting the array. Alternatively, we can choose the pivot at random from all the elements of the array.

6 Randomized Version of QuickSort
Randomized-Quick-Sort(A) Randomized-QSort(A, 1, length[A]) Randomized-QSort(A, lo, hi) if lo < hi p = Randomized-Partition(A, lo, hi) Randomized-QSort(A, lo, p) Randomized-QSort(A, p + 1, hi) Randomized-Partition(A, lo, hi) i = random(lo, hi) // before array is partitioned swap(A, lo, i) // swap A[lo] with an element chosen return Partition(A, lo, hi) // at random from A[lo..hi]. This // makes pivot element equally // likely to be any of the subarray // elements.

7 Worst Case Running Time
How does randomized QuickSort affect the worst case running time? (We will discuss this in class)

8 Analyzing Randomized QuickSort Average Case
Idea: Because the algorithm chooses the pivot at random, we can estimate the probability of each size partition. Chance of a (1, n-1) split is 2/n (if pivot is smallest or next to smallest) Chance of other splits is 1/n each We can establish the following recurrence equation: It can be shown that the solution to the above equation is: T(n) = O(nlgn)

9 Lower bound for comparison based sorts
So far in this class, the best sorting algorithms have all run in O(nlgn) time: merge-sort: Worst case running time: Q(nlgn) heap-sort: Worst case running time O(nlgn) quick-sort: Average case running time O(nlgn) Can we do better? Before we try to find a better comparison based sorting algorithm, can we put a lower bound on all comparison based algorithms?

10 Assumptions Assume the following:
We are given an input sequence <a1, ... , an> Allowable comparisons: ai < aj, ai<=aj, ai=aj ai > aj, ai>=aj No other tools. Assume all the elements ai are distinct. (One can prove the lower bound without this, but it is more complex). We do not need to consider the strict equality, ai = aj Under the above assumption, all of the following comparisons yield identical information regarding the relative order of ai and aj: ai<=aj, ai>=aj, ai<aj, ai>aj Therefore we can assume all comparisons are of the form ai<=aj

11 Decision Tree for n=3 We will work out the decision tree for insertion sort, n = 3.

12 Analyzing the tree Worst case number of comparisons = Length of longest path from root to leaf = height of decision tree. Lower bound on height of the tree = lower bound on the running time of any comparison based algorithm. Height of tree: (We will work this out in class) Are Heap sort and Merge sort asymptotically optimal comparison sorts?


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II"

Similar presentations


Ads by Google