module3

# module3 - QuickSort (review) In QuickSort , we are given an...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QuickSort (review) In QuickSort , we are given an array A of n items. First, we choose an element as the pivot and Partition the array so all elements preceding the pivot are less than the pivot, and all elements following the pivot are greater than the pivot. Then we recursively sort the subarray preceding the pivot and the subarray following the pivot. Algorithm: QuickSort ( A,p,r ) if p < r then q ← Partition ( A,p,r ) QuickSort ( A,p,q- 1) QuickSort ( A,q + 1 ,r ) The initial call is QuickSort ( A, 1 ,n ). 64 / 79 Partition Algorithm: Partition ( A,p,r ) x ← A [ r ] i ← p- 1 for j ← p to r- 1 do if A [ j ] ≤ x then i ← i + 1 Exchange ( A [ i ] ,A [ j ]) Exchange ( A [ i + 1] ,A [ r ]) return ( i + 1) The complexity of Partition is Θ( r- p + 1). 65 / 79 Complexity of QuickSort For simplicity of analysis, assume that all n elements are distinct. Best Case If the pivot always splits the array into two equal parts, then the running time T ( n ) satisfies the following recurrence: T ( n ) = T ( b ( n- 1) / 2 c ) + T ( d ( n- 1) / 2 e ) + Θ( n ) . This is essentially the MergeSort recurrence; its solution is T ( n ) ∈ Θ( n log n ). Worst Case If the pivot is always the maximum (or minimum) element in a subarray, then a subarray of size ‘ is split into subarrays of size 0 and ‘- 1. In this case, we have: T ( n ) = T (0) + T ( n- 1) + Θ( n ) = T ( n- 1) + Θ( n ) . Then T ( n ) ∈ Θ( n 2 ) so the complexity is quadratic. 66 / 79 Average-case Complexity of QuickSort We will estimate the complexity by counting the number of comparisons of two array elements that are performed in the QuickSort algorithm. Since the best-case and worst-case complexities are so different, it is interesting to consider the average-case complexity . What do we mean by “average-case”? The complexity depends only on how the n items are ordered initially (the actual values of the items is immaterial). Therefore we can fix a set of n items (say 1 ,...,n ) and consider all n ! orderings (i.e., permutations) of them. We assume that each ordering is equally likely . For each permutation of the n items, we count the number of comparisons. Then we sum all these numbers and divide by n ! to compute the average....
View Full Document

## This note was uploaded on 02/21/2012 for the course PSYCH 101 taught by Professor Ennis during the Winter '09 term at Waterloo.

### Page1 / 16

module3 - QuickSort (review) In QuickSort , we are given an...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online