Вы находитесь на странице: 1из 3

Quick-Sort 4/9/2002 10:1

Outline and Reading


Š Quick-sort (§4.3)
Quick-Sort „ Algorithm
„ Partition step
7 4 9 6 2 → 2 4 6 7 9 „ Quick-sort tree
„ Execution example
4 2 → 2 4 7 9 → 7 9
Š Analysis of quick-sort (4.3.1)
2→2 9→9 Š In-place quick-sort (§4.8)
Š Summary of sorting algorithms

Quick-Sort 1 Quick-Sort 2

Quick-Sort Partition
Š Quick-sort is a randomized Š We partition an input Algorithm partition(S, p)
sequence as follows: Input sequence S, position p of pivot
sorting algorithm based x „ We remove, in turn, each Output subsequences L, E, G of the
on the divide-and-conquer element y from S and elements of S less than, equal to,
or greater than the pivot, resp.
paradigm: „ We insert y into L, E or G,
L, E, G ← empty sequences
Divide: pick a random depending on the result of
„ x ← S.remove(p)
element x (called pivot) and the comparison with the
x pivot x while ¬S.isEmpty()
partition S into y ← S.remove(S.first())
Š L elements less than x
Š Each insertion and removal
if y < x
is at the beginning or at the
Š E elements equal x L E G L.insertLast(y)
end of a sequence, and
Š G elements greater than x else if y = x
hence takes O(1) time
„ Recur: sort L and G E.insertLast(y)
Š Thus, the partition step of else { y > x }
„ Conquer: join L, E and G x quick-sort takes O(n) time G.insertLast(y)
return L, E, G
Quick-Sort 3 Quick-Sort 4

Quick-Sort Tree Execution Example


Š An execution of quick-sort is depicted by a binary tree
„ Each node represents a recursive call of quick-sort and stores
Š Pivot selection
Š Unsorted sequence before the execution and its pivot
Š Sorted sequence at the end of the execution 7 2 9 43 7 6 1 → 1 2 3 4 6 7 8 9
„ The root is the initial call
„ The leaves are calls on subsequences of size 0 or 1
7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6
7 4 9 6 2 → 2 4 6 7 9

2→2 9 4 → 4 9 3→3 8→8


4 2 → 2 4 7 9 → 7 9

9→9 4→4
2→2 9→9
Quick-Sort 5 Quick-Sort 6
Quick-Sort 4/9/2002 10:1

Execution Example (cont.) Execution Example (cont.)


Š Partition, recursive call, pivot selection Š Partition, recursive call, base case
7 2 9 4 3 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 43 7 6 1→→ 1 2 3 4 6 7 8 9

2 4 3 1→ 2 4 7 9 3 8 6 1 → 1 3 8 6 2 4 3 1 →→ 2 4 7 3 8 6 1 → 1 3 8 6

2→2 9 4 → 4 9 3→3 8→8 1→1 9 4 → 4 9 3→3 8→8

9→9 4→4 9→9 4→4

Quick-Sort 7 Quick-Sort 8

Execution Example (cont.) Execution Example (cont.)


Š Recursive call, …, base case, join Š Recursive call, pivot selection
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9

2 4 3 1 → 1 2 3 4 3 8 6 1 → 1 3 8 6 2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6

1→1 4 3 → 3 4 3→3 8→8 1→1 4 3 → 3 4 8→8 9→9

9→9 4→4 9→9 4→4

Quick-Sort 9 Quick-Sort 10

Execution Example (cont.) Execution Example (cont.)


Š Partition, …, recursive call, base case Š Join, join
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 4 3 7 6 1 →1 2 3 4 6 7 7 9

2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6 2 4 3 1 → 1 2 3 4 7 9 7 → 17 7 9

1→1 4 3 → 3 4 8→8 9→9 1→1 4 3 → 3 4 8→8 9→9

9→9 4→4 9→9 4→4

Quick-Sort 11 Quick-Sort 12
Quick-Sort 4/9/2002 10:1

Worst-case Running Time Expected Running Time


Š The worst case for quick-sort occurs when the pivot is the unique Š Consider a recursive call of quick-sort on a sequence of size s
minimum or maximum element „ Good call: the sizes of L and G are each less than 3s/4
Š One of L and G has size n − 1 and the other has size 0 „ Bad call: one of L and G has size greater than 3s/4
Š The running time is proportional to the sum 7 2 9 43 7 6 19 7 2 9 43 7 6 1

n + (n − 1) + … + 2 + 1
2 4 3 1 7 9 7 1 → 1 1 7294376
Š Thus, the worst-case running time of quick-sort is O(n2)
depth time Good call Bad call

0 n Š A call is good with probability 1/2


„ 1/2 of the possible pivots cause good calls:
1 n−1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

… …
Bad pivots Good pivots Bad pivots
n−1 1
Quick-Sort 13 Quick-Sort 14

Expected Running Time, Part 2 In-Place Quick-Sort


Š Probabilistic Fact: The expected number of coin tosses required in Š Quick-sort can be implemented
order to get k heads is 2k to run in-place
Š For a node of depth i, we expect Š In the partition step, we use Algorithm inPlaceQuickSort(S, l, r)
„ i/2 ancestors are good calls replace operations to rearrange Input sequence S, ranks l and r
„ The size of the input sequence for the current call is at most (3/4)i/2n the elements of the input Output sequence S with the
sequence such that elements of rank between l and r
Š Therefore, we have expected height time per level
rearranged in increasing order
s(r) O(n) „ the elements less than the
„ For a node of depth 2log4/3n, if l ≥ r
the expected input size is one pivot have rank less than h
„ the elements equal to the pivot
return
„ The expected height of the s(a) s(b) O(n)
quick-sort tree is O(log n) have rank between h and k i ← a random integer between l and r
O(log n) „ the elements greater than the x ← S.elemAtRank(i)
Š The amount or work done at the s(c) s(d) s(e) s(f) O(n)
pivot have rank greater than k
nodes of the same depth is O(n) (h, k) ← inPlacePartition(x)
Š The recursive calls consider inPlaceQuickSort(S, l, h − 1)
Š Thus, the expected running time
„ elements with rank less than h inPlaceQuickSort(S, k + 1, r)
of quick-sort is O(n log n)
„ elements with rank greater
total expected time: O(n log n) than k

Quick-Sort 15 Quick-Sort 16

Summary of Sorting Algorithms


Algorithm Time Notes
Š in-place
selection-sort O(n2) Š slow (good for small inputs)
Š in-place
insertion-sort O(n2) Š slow (good for small inputs)
O(n log n) Š in-place, randomized
quick-sort
expected Š fastest (good for large inputs)
Š in-place
heap-sort O(n log n) Š fast (good for large inputs)
Š sequential data access
merge-sort O(n log n) Š fast (good for huge inputs)

Quick-Sort 17

Вам также может понравиться