CH 13
CH 13
Chapter 13:
Searching and Sorting
Program efficiency
algorithm analysis
complexity classes and Big-Oh notation
Note that the values in the array / list are in sorted order.
If they are not, binarySearch is not guaranteed to work
properly.
index 0 1 2 3 4 5 6 7
value 15 2 8 1 17 10 12 5
index 0 1 2 3 4 5 6 7
value 1 2 5 8 10 12 15 17
Output:
[Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel]
Output:
[alpha, bravo, Charlie, DELTA, echo, Foxtrot, golf, hotel]
reading: 13.2
• Which grows
faster?
2 16
5 38
6 43 max
4 37
5 38
6 43
1 7
4 37
5 38
6 43
more specifically:
find the smallest value in the list
switch it with the value in the first position
find the next smallest value in the list
switch it with the value in the second position
repeat until all values are in their proper places
Value
27 63 1 72 64 58 14 9
1st pass
1 63 27 72 64 58 14 9
2nd pass
1 9 27 72 64 58 14 63
3rd pass
1 9 14 72 64 58 27 63
reading: 13.4
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
13 6 21 18 9 4 8 20
0 3 4 7
13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7
13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7
6 13 18 21 4 9 8 20
0 1 2 3 4 5 6 7
6 13 18 21 4 8 9 20
0 3 4 7
4 6 8 9 13 18 20 21
0 7
A 6 13 18 21 B 4 8 9 20
C 4 6 8 9 13 18 20 21