Searching and Sorting Searching and Sorting
Searching and Sorting Searching and Sorting
Searching
Sorted
LINEAR SEARCH
each element in turn to see if its the one
youre looking for
Examine
Original
On
Works
The
The
It
We
It
We
BINARY SEARCH
Method: choose
At
Requires
Very
Twice
PSEUDOCODE
If (range contains only one element):
Look for desired value
Else:
1. Get midpoint of range
2. Determine which half of the range is likely to contain
the desired value
3. Repeat the binary search on just that half of the range
SORTING TECHNIQUES
Many
Ex. Find
51
Start: [4, 10, 13, 14, 22, 29, 35, 37, 44, 51, 63]
Round
Round
Round
These
Different
Some
BUBBLE SORT
PSEUDOCODE
Method: compare
At
This
Each
For
Round 1:
Round 2:
PSEUDOCODE
INSERTION SORT
Method: select
Begin
Slightly
Move
Move
Move
Move
End: [
Blue
SELECTION SORT
search for the largest unsorted item, and
put it into its sorted position
PSEUDOCODE
Repeatedly
Again, we
On
RADIX SORT
Method: Form
This
Worst-case
PSEUDOCODE
AN ILLUSTRATION
Start:
0
1
2
3
4
5
6
7
8
9
4
222, 123
2150, 2154
1560, 1061
283
0
1
2
3
4
5
6
7
8
9
1560, 2150
1061
222
123, 283
2154, 4
0
1
2
3
4
5
6
7
8
9
4, 1061
123, 2150, 2154
222, 283
1560
EXTRACTING DIGITS
0
1
2
3
4
5
6
7
8
9
How
Use
a combination of / and %
Ones
Tens
digit: n % 10
digit: (n / 10) % 10
Hundreds
digit: (n / 100) % 10