Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Quick Sort Algorithm
• Simple, fast, widely used in practice.
• Can be done “in place;” no extra space.
• General Form:
1. Partition: Divide into two subarrays, L and R;
elements in L are all smaller than those in R.
2. Recurse: Sort L and R recursively.
3. Combine: Append R to the end of L.
• Partition (A, p, q, i) partitions A with pivot A[i].

Subhash Suri

UC Santa Barbara
Partition
• Partition returns the index of the cell containing
the pivot in the reorganized array.
11

4

9

7

3

10

2

6

13

21

8

• Example: Partition (A, 0, 10, 3).
• 4, 3, 2, 6, 7, 11, 9, 10, 13, 21, 8

Subhash Suri

UC Santa Barbara
Quick Sort Algorithm
• QuickSort (A, p, q) sorts the subarray A[p · · · q].
• Initial call with p = 0 and q = n − 1.
QuickSort(A, p, q)
if p ≥ q then return
i ← random(p, q)
r ← Partition(A, p, q, i)
Quicksort (A, p, r − 1)
Quicksort (A, r + 1, q)

Subhash Suri

UC Santa Barbara
Analysis of QuickSort
• Lucky Case: Each Partition splits array in halves.
We get T (n) = 2T (n/2) + Θ(n) = Θ(n log n).
• Unlucky Case: Each partition gives unbalanced
split. We get T (n) = T (n − 1) + Θ(n) = Θ(n2).
• In worst case, Quick Sort as bad as BubbleSort.
The worst-case occurs when the list is already
sorted, and the last element chosen as pivot.
• But, while BubbleSort always performs poorly on
certain inputs, because of random pivot,
QuickSort has a chance of doing much better.
Subhash Suri

UC Santa Barbara
Analyzing QuickSort
• T (n): runtime of randomized QuickSort.
• Assume all elements are distinct.
• Recurrence for T (n) depends on two subproblem
sizes, which depend on random partition element.
• If pivot is i smallest element, then exactly (i − 1)
items in L and (n − i) in R. Call it an i-split.
• What’s the probability of i-split?
• Each element equally likely to be chosen as pivot,
1
so the answer is n .
Subhash Suri

UC Santa Barbara
Solving the Recurrence
n

1
T (n) =
(runtime with i-split) + n + 1
n
i=1
n

1
=
(T (i − 1) + T (n − i)) + n + 1
n i=1
n

2
T (i − 1) + n + 1
=
n i=1
2
=
n

Subhash Suri

n−1

T (i) + n + 1
i=0

UC Santa Barbara
Solving the Recurrence
• Multiply both sides by n. Subtract the same
formula for n − 1.
n−1

T (i) + n2 + n

nT (n) = 2
i=0
n−2

T (i) + (n − 1)2 + (n − 1)

(n − 1)T (n − 1) = 2
i=0

Subhash Suri

UC Santa Barbara
Solving the Recurrence
nT (n) = (n + 1)T (n − 1) + 2n
T (n − 1)
2
T (n)
=
+
n+1
n
n+1
T (n − 2) 2
2
=
+ +
n−1
n n+1
.
.
n
2
T (2)
+
=
3
i
i=3
= Θ(1) + 2 ln n
• Thus, T (n) ≤ 2(n + 1) ln n.
Subhash Suri

UC Santa Barbara

More Related Content

Quick sort algorithn

  • 1. Quick Sort Algorithm • Simple, fast, widely used in practice. • Can be done “in place;” no extra space. • General Form: 1. Partition: Divide into two subarrays, L and R; elements in L are all smaller than those in R. 2. Recurse: Sort L and R recursively. 3. Combine: Append R to the end of L. • Partition (A, p, q, i) partitions A with pivot A[i]. Subhash Suri UC Santa Barbara
  • 2. Partition • Partition returns the index of the cell containing the pivot in the reorganized array. 11 4 9 7 3 10 2 6 13 21 8 • Example: Partition (A, 0, 10, 3). • 4, 3, 2, 6, 7, 11, 9, 10, 13, 21, 8 Subhash Suri UC Santa Barbara
  • 3. Quick Sort Algorithm • QuickSort (A, p, q) sorts the subarray A[p · · · q]. • Initial call with p = 0 and q = n − 1. QuickSort(A, p, q) if p ≥ q then return i ← random(p, q) r ← Partition(A, p, q, i) Quicksort (A, p, r − 1) Quicksort (A, r + 1, q) Subhash Suri UC Santa Barbara
  • 4. Analysis of QuickSort • Lucky Case: Each Partition splits array in halves. We get T (n) = 2T (n/2) + Θ(n) = Θ(n log n). • Unlucky Case: Each partition gives unbalanced split. We get T (n) = T (n − 1) + Θ(n) = Θ(n2). • In worst case, Quick Sort as bad as BubbleSort. The worst-case occurs when the list is already sorted, and the last element chosen as pivot. • But, while BubbleSort always performs poorly on certain inputs, because of random pivot, QuickSort has a chance of doing much better. Subhash Suri UC Santa Barbara
  • 5. Analyzing QuickSort • T (n): runtime of randomized QuickSort. • Assume all elements are distinct. • Recurrence for T (n) depends on two subproblem sizes, which depend on random partition element. • If pivot is i smallest element, then exactly (i − 1) items in L and (n − i) in R. Call it an i-split. • What’s the probability of i-split? • Each element equally likely to be chosen as pivot, 1 so the answer is n . Subhash Suri UC Santa Barbara
  • 6. Solving the Recurrence n 1 T (n) = (runtime with i-split) + n + 1 n i=1 n 1 = (T (i − 1) + T (n − i)) + n + 1 n i=1 n 2 T (i − 1) + n + 1 = n i=1 2 = n Subhash Suri n−1 T (i) + n + 1 i=0 UC Santa Barbara
  • 7. Solving the Recurrence • Multiply both sides by n. Subtract the same formula for n − 1. n−1 T (i) + n2 + n nT (n) = 2 i=0 n−2 T (i) + (n − 1)2 + (n − 1) (n − 1)T (n − 1) = 2 i=0 Subhash Suri UC Santa Barbara
  • 8. Solving the Recurrence nT (n) = (n + 1)T (n − 1) + 2n T (n − 1) 2 T (n) = + n+1 n n+1 T (n − 2) 2 2 = + + n−1 n n+1 . . n 2 T (2) + = 3 i i=3 = Θ(1) + 2 ln n • Thus, T (n) ≤ 2(n + 1) ln n. Subhash Suri UC Santa Barbara