Data Structures in Java
Lecture 14: Sorting I
11/9/2015 Daniel Bauer
1
Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel - - PowerPoint PPT Presentation
Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1 Sorting Midterm Exams 2 Sorting Input: 34 8 64 51 32 21 Array containing unordered Comparables (duplicates allowed). Output: 8 21 32 34 51 64
Lecture 14: Sorting I
11/9/2015 Daniel Bauer
1
2
(duplicates allowed).
(comparison based sorting).
34 8 64 51 32 21 8 21 32 34 51 64
3
4
4
4
4
4
4
4
running time, required space, and stability.
5
34 8 64 51 32 21
it is in the correct position. p=1
6
34 8 64 51 32 21
it is in the correct position. p=1
6
34 8 64 51 32 21
it is in the correct position.
8
p=2
64
6
34 8 32 21
it is in the correct position.
8 64
p=3
51
6
34 8 32 21
it is in the correct position.
8 64
p=3
51
6
34 8 21
it is in the correct position.
8 64 51 32
p=4
6
34 8 21
it is in the correct position.
8 64 51 32
p=4
6
34 8 21
it is in the correct position.
8 64 51 32
p=4
6
34 8 21
it is in the correct position.
8 64 51 32
p=4
6
34 8
it is in the correct position.
8 64 51 32 21
p=5
6
34 8
it is in the correct position.
8 64 51 32 21
p=5
6
34 8
it is in the correct position.
8 64 51 32 21
p=5
6
34 8
it is in the correct position.
8 64 51 32 21
p=5
6
34 8
it is in the correct position.
8 64 51 32 21
p=5
6
void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-‑ ¡1]; ¡j-‑-‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }
7
void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-‑ ¡1]; ¡j-‑-‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }
O(N) O(N)
Total: O(N2)
7
void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-‑ ¡1]; ¡j-‑-‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }
O(N) O(N)
Total: O(N2) Best case input (sorted): O(N)
7
void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-‑ ¡1]; ¡j-‑-‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }
O(N) O(N)
Total: O(N2) Worst case input (sorted in reverse order): Best case input (sorted): O(N)
7
further apart can be swapped.
sure that all items space hk apart are sorted.
34 8 64 51 32 21 7 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
8
further apart can be swapped.
sure that all items space hk apart are sorted.
34 8 64 51 32 21 7 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
8
further apart can be swapped.
sure that all items space hk apart are sorted.
21 8 64 51 32 34 7 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
9
further apart can be swapped.
sure that all items space hk apart are sorted.
21 8 64 51 32 34 7 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
9
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 64 51 32 34 8 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
10
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 64 51 32 34 8 30 1
h3 = 5
2 5
h2 = 3 h1 = 1
10
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 51 32 34 8 64 1
h3 = 5
2 5
h2 = 3 h1 = 1
11
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 51 32 34 8 64 1
h3 = 5
2 5
h2 = 3 h1 = 1
11
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 1 32 34 8 64 51
h3 = 5
2 5
h2 = 3 h1 = 1
12
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 1 32 34 8 64 51
h3 = 5
2 5
h2 = 3 h1 = 1
12
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 1 2 34 8 64 51
h3 = 5
32 5
h2 = 3 h1 = 1
13
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 1 2 34 8 64 51
h3 = 5
32 5
h2 = 3 h1 = 1
13
further apart can be swapped.
sure that all items space hk apart are sorted.
21 7 30 1 2 34 8 64 51
h3 = 5
32 5
h2 = 3 h1 = 1
13
further apart can be swapped.
sure that all items space hk apart are sorted.
5 7 30 1 2 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
14
further apart can be swapped.
sure that all items space hk apart are sorted.
5 7 30 1 2 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
14
further apart can be swapped.
sure that all items space hk apart are sorted.
1 7 30 5 2 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
15
further apart can be swapped.
sure that all items space hk apart are sorted.
1 7 30 5 2 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
15
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 30 5 7 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
16
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 30 5 7 21 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
16
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
17
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
18
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
19
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
20
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
21
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 64 51
h3 = 5
32 34
h2 = 3 h1 = 1
21
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
22
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
23
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
24
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 21 5 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
24
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 21 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
25
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 21 7 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
25
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 21 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
26
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 21 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
27
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 21 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
27
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 21 30 8 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
27
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
28
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
29
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
30
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
30
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 34 51
h3 = 5
32 64
h2 = 3 h1 = 1
30
further apart can be swapped.
sure that all items space hk apart are sorted.
1 2 5 7 8 21 30 32 34
h3 = 5
51 64
h2 = 3 h1 = 1
31
and depends on the specific increment sequence.
worst case running time is
32
array.
for the same key may be in different order than in the input.
34 8 64 1 4 3 30 7 2 5
val1
1
val233
array.
for the same key may be in different order than in the input.
34 8 64 1 4 3 30 7 2 5
val1
1
val2
33
temporary value for swaps.
34
O(N) time.
the elements in sorted order.
35
O(N) time.
the elements in sorted order.
store the output: O(N) space!
35
5 4 6 9 1 8 3 10 7 2 11
36
1 2 3 4 5 6 7 8 9
10 11
5 4 6 9 1 8 3 10 7 2 11
36
6 3 7 8 9
10
2 1 4 5
11
1 2 3 7 4 8 6 10 9 5 11
Build heap in O(N) time
37
6 3 7 8 9
10
2 1 4 5
11
1 2 3 7 4 8 6 10 9 5 11
Build heap in O(N) time
37
6 3 7 8 9
10
2 1 4 5
11
2 3 7 4 8 6 10 9 5
1 11 deleteMin, write min element into empty cell
38
6 3 7 8 9
10
2 4 5
11
2 3 7 4 8 6 10 9 5
1 11 deleteMin, write min element into empty cell
38
6 3 7 8 9
10
4 2 5 11
2 4 3 7 5 8 6 10 9 11
1 Percolate down
39
6 3 7 8 9
10
4 2 5 11
4 3 7 5 8 6 10 9
1 11 2 deleteMin, write min element into empty cell
40
6 3 7 8 9
10
4 5 11
4 3 7 5 8 6 10 9
1 11 2 deleteMin, write min element into empty cell
40
11 6 7 8 9
10
4 3 5
4 6 7 5 8 11 10 9
1 2 3 Percolate down
41
11 6 7 8 9
10
4 5
5 6 7 9 8 11 10 3
1 2 4 deleteMin, write min element into empty cell
42
11 6 10 8 9 5 7
7 6 10 9 8 11 4 3
1 2 5 deleteMin, write min element into empty cell
43
8 10 11 9 6 7
7 8 10 9 11 5 4 3
1 2 6 deleteMin, write min element into empty cell
44
8 10 11 7 9
9 8 10 11 6 5 4 3
1 2 7 deleteMin, write min element into empty cell
45
11 10 8 9
9 11 10 7 6 5 4 3
1 2 8 deleteMin, write min element into empty cell
46
11 9 10
10 11 8 7 6 5 4 3
1 2 9 deleteMin, write min element into empty cell
47
10 11
11 9 8 7 6 5 4 3
1 2 10 deleteMin, write min element into empty cell
48
11
10 9 8 7 6 5 4 3
1 2 11
increasing order.
49
51 32 21 1 34 8 64 2
50
51 32 21 1 34 8 64 2
50
1 21 32 51 2 8 34 64
51
1 21 32 51 2 8 34 64
1 2 8 21 32 34 51 64
51
1 21 32 51 2 8 34 64
Actr Bctr Cctr
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1 2
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1 2 8
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1 2 8 21
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1 2 8 21 32
tmp a
52
1 21 32 51 2 8 34 64
Actr Bctr Cctr
1 2 8 21 32 34
tmp a
52
1 21 32 51 2 8 34 64
Actr Cctr
1 2 8 21 32 34 51
tmp a
52
1 21 32 51 2 8 34 64
1 2 8 21 32 34 51 64
tmp a
52
¡ ¡ ¡ ¡private ¡static ¡<T ¡extends ¡Comparable<T>> ¡ ¡ ¡ ¡ ¡void ¡merge( ¡T[] ¡a, ¡T[] ¡tmpArray, ¡int ¡aCtr, ¡int ¡bCtr, ¡int ¡rightEnd ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡leftEnd ¡= ¡bCtr ¡-‑ ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡tmpPos ¡= ¡aCtr; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡numElements ¡= ¡rightEnd ¡-‑ ¡aCtr ¡+ ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Main ¡loop ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡aCtr ¡<= ¡leftEnd ¡&& ¡bCtr ¡<= ¡rightEnd ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡a[ ¡aCtr ¡].compareTo( ¡a[ ¡bCtr ¡] ¡) ¡<= ¡0 ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡aCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡bCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡aCtr ¡<= ¡leftEnd ¡) ¡ ¡ ¡ ¡// ¡Copy ¡rest ¡of ¡first ¡half ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡aCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡bCtr ¡<= ¡rightEnd ¡) ¡ ¡// ¡Copy ¡rest ¡of ¡right ¡half ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡bCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Copy ¡tmpArray ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡int ¡i ¡= ¡0; ¡i ¡< ¡numElements; ¡i++, ¡rightEnd-‑-‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡rightEnd ¡] ¡= ¡tmpArray[ ¡rightEnd ¡]; ¡ ¡ ¡ ¡ ¡}
53
51 32 21 1 34 8 64 2
54
51 32 21 1 34 8 64 2
55
51 32 21 1 34 8 64 2
56
51 32 21 1 34 8 64 2 8 34 2 64 32 51 1 21
57
51 32 21 1 34 8 64 2 8 34 2 64 32 51 1 21 2 8 34 64 1 21 32 51
58
51 32 21 1 34 8 64 2 8 34 2 64 32 51 1 21 2 8 34 64 1 21 32 51 1 2 8 21 32 34 51 64
59
private ¡static ¡<T ¡extends ¡Comparable<T>> ¡ void ¡mergeSort( ¡T[] ¡a, ¡T[] ¡tmpArray, ¡int ¡left, ¡int ¡right ¡) ¡ ¡ ¡ ¡ ¡ ¡if( ¡left ¡< ¡right ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡center ¡= ¡( ¡left ¡+ ¡right ¡) ¡/ ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡mergeSort( ¡a, ¡tmpArray, ¡left, ¡center ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡mergeSort( ¡a, ¡tmpArray, ¡center ¡+ ¡1, ¡right ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡merge( ¡a, ¡tmpArray, ¡left, ¡center ¡+ ¡1, ¡right ¡); ¡ ¡ ¡ ¡ ¡} ¡ }
60
conquer algorithms.
time analysis should be similar to what we have seen for other algorithms of this type (e.g. binary search)
Recursively sort each half Merge the two halfs
61
assume
62
63
63
Need a temporary array. O(N)
63