Data Structures in Java
Lecture 15: Sorting II
11/11/2015 Daniel Bauer
1
Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel - - PowerPoint PPT Presentation
Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel Bauer 1 Quick Sort Another divide-and-conquer algorithm. Pick any pivot element v. Partition the array into elements x v and x v. Recursively sort the
Lecture 15: Sorting II
11/11/2015 Daniel Bauer
1
51 32 21 1 34 8 64 2
2
51 32 21 1 34 8 64 2 21
v
2
51 32 21 1 34 8 64 2 21
v
1 8 2
x ≤ v
2
51 32 21 1 34 8 64 2 21
v
1 8 2
x ≤ v
51 32 34 64
x ≥ v
2
51 32 21 1 34 8 64 2 21
v
8 1 2
x ≤ v
51 64 32 34
x ≥ v
3
51 32 21 1 34 8 64 2 21
v
8 1 2
x ≤ v
51 64 32 34
x ≥ v
3
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64
4
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64
5
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64 32 34
6
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64 32 34
6
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34
7
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34
7
51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34
7
51 32 21 1 34 8 64 2 21 8 1 2 51 64 32 34
8
51 32 21 1 34 8 64 2 21 8 1 2 51 64 32 34
8
32 34 51 64 1 2 8 21
depends on our choice.
9
partition the array in place.
elements x ≥ v to the right.
51 32 21 1 34 8 64 2
10
partition the array in place.
elements x ≥ v to the right.
51 32 21 1 34 8 64 2
Move the pivot to the end.
10
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
51 1 21 32 34 8 64 2
i j
11
¡ ¡ ¡ ¡public ¡static ¡void ¡quicksort(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(right ¡> ¡left) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡v ¡= ¡find_pivot_index(a, ¡left, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡int ¡j ¡= ¡right-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡to ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp ¡= ¡a[v]; ¡a[v] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡{ ¡// ¡partition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++i] ¡< ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++j] ¡> ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(i ¡>= ¡j) ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[j]; ¡a[j] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡//recursively ¡sort ¡both ¡partitions ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡quicksort(a,left, ¡i-‑1); ¡ ¡quicksort(a,i+1, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡}
12
¡ ¡ ¡ ¡public ¡static ¡void ¡quicksort(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(right ¡> ¡left) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡v ¡= ¡find_pivot_index(a, ¡left, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡int ¡j ¡= ¡right-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡to ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp ¡= ¡a[v]; ¡a[v] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡{ ¡// ¡partition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++i] ¡< ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++j] ¡> ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(i ¡>= ¡j) ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[j]; ¡a[j] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡//recursively ¡sort ¡both ¡partitions ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡quicksort(a,left, ¡i-‑1); ¡ ¡quicksort(a,i+1, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡}
O(N)
12
partitions the array.
51 32 21 1 34 8 64 2
. . . 13
partitions the array.
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1
. . . 13
partitions the array.
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
. . . 13
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
⁞
51 64 51 64
T(1) = 1
14
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
⁞
51 64 51 64
T(1) = 1 T(2) = T(1) + 2
Time for partitioning
14
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
⁞
51 64 51 64
T(1) = 1 T(2) = T(1) + 2
Time for partitioning
T(N-2) = T(N-3) + (N-2)
14
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
⁞
51 64 51 64
T(1) = 1 T(2) = T(1) + 2
Time for partitioning
T(N-1) = T(N-2) + (N-1) T(N-2) = T(N-3) + (N-2)
14
51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64
⁞
51 64 51 64
T(1) = 1 T(2) = T(1) + 2 T(N) = T(N-1) + N
Time for partitioning
T(N-1) = T(N-2) + (N-1) T(N-2) = T(N-3) + (N-2)
14
15
15
15
15
15
Both partitions have about the same size.
51 32 21 1 34 8 64 2
16
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2
16
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2
16
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64
16
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64
T(N) = 2 T(N/2) +N (we ignore the pivot element, so this overestimates the running time slightly)
17
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64
T(N) = 2 T(N/2) +N T(N/2) = 2 T(N/4) +N/2 (we ignore the pivot element, so this overestimates the running time slightly)
17
Both partitions have about the same size.
51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64
T(N) = 2 T(N/2) +N T(N/2) = 2 T(N/4) +N/2 T(1) = 1
⁞
(we ignore the pivot element, so this overestimates the running time slightly)
17
(note that this is the same analysis as for Merge Sort)
18
(note that this is the same analysis as for Merge Sort)
18
(note that this is the same analysis as for Merge Sort)
18
assume
(note that this is the same analysis as for Merge Sort)
18
assume
(note that this is the same analysis as for Merge Sort)
18
assume
(note that this is the same analysis as for Merge Sort)
18
19
partition, but we don’t know where it is!
19
partition, but we don’t know where it is!
19
partition, but we don’t know where it is!
is a terrible idea! Better: Choose a random element.
19
partition, but we don’t know where it is!
is a terrible idea! Better: Choose a random element.
19
51 32 21 1 34 8 64 2
Choose the median of array[0], array[n]m and array[n/2].
20
51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64
Choose the median of array[0], array[n]m and array[n/2].
20
51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64 51 32 21 34 8 64
Choose the median of array[0], array[n]m and array[n/2].
20
51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64 51 32 21 34 8 64
Choose the median of array[0], array[n]m and array[n/2].
21 8 32
20
¡ ¡ ¡ ¡public ¡static ¡int ¡find_pivot_index(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡center ¡= ¡( ¡left ¡+ ¡right ¡) ¡/ ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[center] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[center]; ¡a[center] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[center]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[center]; ¡a[center] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡center; ¡ ¡ ¡ ¡ ¡}
21
22
(but can make QuickSort stable).
22
(but can make QuickSort stable). In-place O(1), but the method activation stack grows with the running time. O(N)
22
TWorst TBest TAvg Space Stable?
Insertion Sort
✓
Shell Sort
✗
Heap Sort
✗
Merge Sort
✓
Quick Sort
✗
*depends on increment sequence
gray entries: not shown in class
23
TWorst TBest TAvg Space Stable?
Insertion Sort
✓
Shell Sort
✗
Heap Sort
✗
Merge Sort
✓
Quick Sort
✗
*depends on increment sequence
gray entries: not shown in class
worst case lower bound on comparison based general sorting! Can we do better if we make some assumptions?
23
increment count[Ai].
2 4 6 1 1 8 2 3
A count 1 2 3 4 5 6 7 8 9
24
increment count[Ai].
2 4 6 1 1 8 2 3
A
1
count 1 2 3 4 5 6 7 8 9
25
increment count[Ai].
2 4 6 1 1 8 2 3
A
1
count 1 2 3 4 5 6 7 8 9
1
26
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1
count 1 2 3 4 5 6 7 8 9
1
27
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1 1
count 1 2 3 4 5 6 7 8 9
1
28
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 2 1
count 1 2 3 4 5 6 7 8 9
1
29
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1 2 1
count 1 2 3 4 5 6 7 8 9
1
30
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1 1 2 1
count 1 2 3 4 5 6 7 8 9
1
31
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
32
increment count[Ai].
2 4 6 1 1 8 2 3
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
32
count[i] copies of i to A. A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
33
count[i] copies of i to A.
1 1
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
34
count[i] copies of i to A.
1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
35
count[i] copies of i to A.
3 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
36
count[i] copies of i to A.
3 4 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
37
count[i] copies of i to A.
3 4 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
38
count[i] copies of i to A.
3 4 6 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
39
count[i] copies of i to A.
3 4 6 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
40
count[i] copies of i to A.
3 4 6 8 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
41
count[i] copies of i to A.
3 4 6 8 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
42
count[i] copies of i to A.
3 4 6 8 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
42
count[i] copies of i to A.
3 4 6 8 1 1 2 2
A
1 1 2 2 1
count 1 2 3 4 5 6 7 8 9
1
42
(e.g. all base-10 integers up to 103)
each digit.
064 008 216 512 027 729 000 001 343 125
43
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064
to least significant digit.
44
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008
to least significant digit.
45
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216
to least significant digit.
46
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512
to least significant digit.
47
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027
to least significant digit.
48
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729
to least significant digit.
49
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729 000
to least significant digit.
50
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729 000 001
to least significant digit.
51
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729 000 001 003
to least significant digit.
52
064 008 216 512 027 729 000 001 343 125
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729 000 001 003 125
to least significant digit.
53
1 2 3 4 5 6 7 8 9
064 008 216 512 027 729 000 001 003 125 000 001 512 343 064 125 216 027 008 729
54
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000
to second-least significant digit.
55
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001
to second-least significant digit.
56
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512
to second-least significant digit.
57
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343
to second-least significant digit.
58
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064
to second-least significant digit.
59
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125
to second-least significant digit.
60
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216
to second-least significant digit.
61
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027
to second-least significant digit.
62
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027 008
to second-least significant digit.
63
1 2 3 4 5 6 7 8 9
000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027 008 729
to second-least significant digit.
64
1 2 3 4 5 6 7 8 9
000 001 008 512 216 125 027 729 343 064 000 001 512 343 064 125 216 027 008 729
65
1 2 3 4 5 6 7 8 9
000 001 008 512 216 125 027 729 343 064
to third-least significant digit. 000 001 008 512 216 125 027 729 343 064
66
1 2 3 4 5 6 7 8 9
000 001 008 027 064 125 216 343 512 729 000 001 008 512 216 125 027 729 343 064
67
1 2 3 4 5 6 7 8 9
000 001 008 027 064 125 216 343 512 729 000 001 008 512 216 125 027 729 343 064
67
a b c d e
… 97 98 99 100 101 … 256 bad fad bid die pie pre dna bob nib sic
by in
68