j - Bilkent University Computer Engineering Department

Transkript

j - Bilkent University Computer Engineering Department
CS473-Algorithms I
Lecture 5
Quicksort
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
1
Quicksort
• Proposed by C.A.R. Hoare in 1962.
• Divide-and-conquer algorithm.
• Sorts “in place” (like insertion sort, but
not like merge sort).
• Very practical (with tuning).
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
2
Quicksort
1. Divide: Partition the array into 2 subarrays such
that elements in the lower part  elements in the
higher part
x
p
x
q
r
2. Conquer: Recursively sort 2 subarrays
3. Combine: Trivial (because in-place)
• Key: Linear-time ((n)) partitioning algorithm
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
3
Two partitioning algorithms
1. Hoare’s algorithm: Partitions around the first
element of subarray (pivot  x  A[p])
x
p
x
?
i
j
r
2. Lomuto’s algorithm: Partitions around the last
element of subarray (pivot  x  A[r])
x
p
CS473 – Lecture 5
>x
i
?
j
Cevdet Aykanat - Bilkent University
Computer Engineering Department
x
r
4
Hoare’s Partitioning Algorithm
H-PARTITION (A, p, r)
pivot  A[p]
Running
ip1
is O(n)
jr1
while true do
repeat j  j  1 until A[j]  pivot
repeat i  i  1 until A[i]  pivot
if i < j then
exchange A[i]  A[j]
else
return j
x
p
CS473 – Lecture 5
x
?
i
time
j
Cevdet Aykanat - Bilkent University
Computer Engineering Department
r
5
QUICKSORT (A, p, r)
if p < r then
q  H-PARTITION(A, p, r)
QUICKSORT(A, p, q)
QUICKSORT(A, q 1, r)
Initial invocation: QUICKSORT(A, 1, n)
x
p
CS473 – Lecture 5
x
q
Cevdet Aykanat - Bilkent University
Computer Engineering Department
r
6
Hoare’s Partitioning Algorithm
• Select a pivot element: pivotA[p] from A[p…r]
• Grows two regions
A[p…i] from left to right
A[j…r] from right to left
such that
every element in A[p…i] is  pivot
every element in A[j…r] is  pivot
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
7
Hoare’s Partitioning Algorithm
• The two regions A[p…i] and A[j…r] grow until
A[i]  pivot  A[j]
• Assuming these inequalities are strict
 A[i] is too large to belong to the left region
 A[j] is too small to belong to the right region
 exchange A[i]A[j] for future growing in the
next iteration
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
8
Hoare’s Partitioning Algorithm
• It is important that
 A[p] is chosen as the pivot element
 If A[r] is used as pivot then
• may yield a trivial split (termination i  j  r)
• occurs when A[p…r1] < pivot  A[r]
• then quicksort may loop forever since q  r
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
9
Hoare’s Algorithm: Example 1 (pivot = 5)
5
3
2
6
4
1
3
7
j
i
5
i
3
2
6
4
1
3
j
7
3
i
3
2
6
4
1
5
j
7
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
10
Hoare’s Algorithm: Example 1 (pivot = 5)
3
3
2
6
i
4
1
j
5
7
3
3
2
1
i
4
6
j
5
7
p
3
q
3
2
1
4
j
Termination: i  6; j  5,
CS473 – Lecture 5
r
6
5
7
i
i.e., i  j  1
Cevdet Aykanat - Bilkent University
Computer Engineering Department
11
Hoare’s Algorithm: Example 2 (pivot = 5)
5
3
2
6
5
1
3
7
j
i
5
i
3
2
6
5
1
3
j
7
3
i
3
2
6
5
1
5
j
7
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
12
Hoare’s Algorithm: Example 2 (pivot = 5)
3
3
2
6
i
5
1
j
5
7
3
3
2
1
i
5
6
j
5
7
p
3
q
3
2
1
5
j
i
r
6
5
7
Termination: i  j  5
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
13
Correctness of Hoare’s Algorithm
(a) Indices i & j never reference A outside the interval A[p…r]
(b) Split is always non-trivial; i.e., j  r at termination
(c) Every element in A[p…j]  every element in A[ j 1…r ] at
termination
Notation used for proof:
• k  # of times while-loop iterates until termination
•
il & jl  values of i & j indices at the end of iteration 1  l  k
•
Note: we always have i1  p & p  j1  r
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
14
Correctness of Hoare’s Algorithm
Lemma: Either ik  jk or ik  jk 1 at termination
 k  1: occurs when A[p+1…r] > pivot  il  jl  p
>x
p
i
r
j1
i1
CS473 – Lecture 5
x  pivot
j
Cevdet Aykanat - Bilkent University
Computer Engineering Department
15
Correctness of Hoare’s Algorithm
 k > 1: we have ik-1 < jk-1
(A[ik-1]  pivot & A[jk-1]  pivot due to exchange)
x  pivot
• case a: ik-1< jk < jk-1
 case a-1: A[jk]  pivot  ik  jk
x
<x
p
ik-1
CS473 – Lecture 5
x
x
>x
jk
ik
Cevdet Aykanat - Bilkent University
Computer Engineering Department
jk-1
r
16
x  pivot
 case a-2: A[jk] < pivot  ik  jk  1
<x
<x
>x
x
p
jk
ik-1
ik
• case b: ik-1  jk < jk-1  ik  jk 1
x
p
r
jk-1
x  pivot
x
>x
jk
ik-1
CS473 – Lecture 5
x
jk-1
ik
Cevdet Aykanat - Bilkent University
Computer Engineering Department
r
17
Correctness of Hoare’s Algorithm
(a) Indices i & j never reference A outside the interval A[p…r]
(b) Split is always non-trivial; i.e., j  r at termination
Proof of (a) & (b)
•
k  1: trivial since i1  j1  p
•
k > 1: p  jk < r  p < ik  r due to the lemma
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
18
(c) Every element in A[p…j]  every element in A[ j 1…r ] at
termination
Proof of (c) : by induction on l (while-loop iteration sequence)
Basis: true for l  1
p  i1  j1
r
• k  1:
L1pivot
p
•
R1>pivot
exchange
k > 1:
L1pivot
r
?
 pivot
pivot
after exchange
CS473 – Lecture 5
j1
Cevdet Aykanat - Bilkent University
Computer Engineering Department
>pivot
R1pivot
19
•
Hypothesis: Ll-1  A[p… il-1]  pivot  A[jl-1…r]  Rl-1
•
Show that
 Ll  A[p… il]  pivot  A[jl…r]  Rl if l < k
 Ll  A[p… jl]  pivot  A[jl 1…r]  Rl if l  k
•
Case: l < k (partition does not terminate at iteration l)
Ll  pivot
Rl  pivot
Ll-1  pivot <pivot
>pivot
Rl-1  pivot
?
p
il-1
il
jl
 pivot  pivot
jl-1
r
after exchange
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
20
Lomuto’s Partitioning Algorithm
L-PARTITION (A, p, r)
pivot  A[r]
ip1
for j  p to r  1 do
if A[j]  pivot then
ii1
exchange A[i]  A[j]
exchange A[i  1]  A[r]
return i + 1
x
p
CS473 – Lecture 5
Running time
is O(n)
>x
i
?
j
Cevdet Aykanat - Bilkent University
Computer Engineering Department
x
r
21
QUICKSORT (A, p, r)
if p < r then
q  L-PARTITION(A, p, r)
QUICKSORT(A, p, q  1)
QUICKSORT(A, q 1, r)
Initial invocation: QUICKSORT(A, 1, n)
x
p
CS473 – Lecture 5
x
>x
q
Cevdet Aykanat - Bilkent University
Computer Engineering Department
r
22
Lomuto’s Algorithm: Example (pivot = 4)
i
7
j
8
2
6
5
1
3
4
7
i
8
2
6
5
1
3
4
2
i
8
6
5
1
3
4
CS473 – Lecture 5
j
7
j
Cevdet Aykanat - Bilkent University
Computer Engineering Department
23
Lomuto’s Algorithm: Example (pivot = 4)
2
8
i
7
6
5
1
j
3
4
2
1
7
6
5
8
3
4
3
j
4
i
2
CS473 – Lecture 5
1
j
7
i
6
5
8
Cevdet Aykanat - Bilkent University
Computer Engineering Department
24
Example: pivot = 4
2
1
3
i
6
5
8
7
j
4
2
1
3
6
5
8
7
4
i
2
p
CS473 – Lecture 5
1
3
r
4
q
5
8
Cevdet Aykanat - Bilkent University
Computer Engineering Department
7
6
r
25
Comparison of Hoare’s & Lomuto’s Algorithms
Notation: n  rp+1 & pivot  A[p] (Hoare)
& pivot  A[r] (Lomuto)
 # of element exchanges: e(n)
• Hoare: 0  e(n)   n 
2
 Best: k 1 with i1j1p (i.e., A[ p+1…r ] > pivot)
 Worst: A[ p+1…p+  n  1]  pivot  A[ p+  n …r ]
2
2
• Lomuto: 1  e(n)  n
 Best: A[ p…r 1 ] > pivot
 Worst: A[ p…r 1 ]  pivot
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
26
Comparison of Hoare’s & Lomuto’s Algorithms
 # of element comparisons: ce(n)
• Hoare: n + 1  ce(n)  n + 2
 Best: ik  jk
 Worst: ik  jk + 1
• Lomuto: ce(n) = n  1
 # of index comparisons: ci(n)
n
• Hoare: 1  ci(n)   2  + 1 (ci(n)  e(n) + 1)
 
• Lomuto: ci(n)  n  1
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
27
Comparison of Hoare’s & Lomuto’s Algorithms
 # of index increment/decrement operations: a(n)
• Hoare: n + 1  a(n)  n + 2 (a(n)  ce(n))
• Lomuto: n  a(n)  2n  1 (a(n)  e(n) + (n  1))
•
Hoare’s algorithm is in general faster
•
Hoare behaves better when pivot is repeated in A[p…r]
 Hoare: Evenly distributes them between left & right
regions
 Lomuto: Puts all of them to the left region
CS473 – Lecture 5
Cevdet Aykanat - Bilkent University
Computer Engineering Department
28

Benzer belgeler

CS473-Algorithms I - Bilkent University Computer Engineering

CS473-Algorithms I - Bilkent University Computer Engineering *Can modify any algorithm (almost) to have a low best-case running time – Check whether input constitutes an output at the very beginning of the algorithm

Detaylı

n - Bilkent University Computer Engineering Department

n - Bilkent University Computer Engineering Department • IDEA: Strengthen the inductive hypothesis. • Subtract a low-order term. Inductive hypothesis: T(k)  c1k2 – c2k for k < n T(n) = 4T(n/2) + n  4 (c1 (n/2)2- c2 (n/2)) + n = c1 n2- 2 c2 n + n = c1...

Detaylı

Bilkent University Computer Engineering Department

Bilkent University Computer Engineering Department Computing the Optimal Subset-Sum Value SUBSET-SUM (x, n, B) for b ← 0 to B do c[0, b] ← 0 for i ← 1 to n do c[i, 0] ← 0 for i ← 1 to n do for b ← 1 to B do if xi ≤ b then c[i, b] ← Max{xi + c[i-1,...

Detaylı

Veri Yapıları - Ders Notu - Süper

Veri Yapıları - Ders Notu - Süper enqueue(queue,count,top,100); // kuyruğa at int value; dequeue(queue,count,bottom,top,value); // kuyruktan al

Detaylı

n - Bilkent University Computer Engineering Department

n - Bilkent University Computer Engineering Department • The two regions A[p…i] and A[j…r] grow until A[i]  pivot  A[j] • Assuming these inequalities are strict  A[i] is too large to belong to the left region  A[j] is too small to belong to the rig...

Detaylı