Chapter Two Searching and Sorting: Algorithm
Chapter Two Searching and Sorting: Algorithm
4
Linear (Serial) Search
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
– record with matching key is found
– or when search has examined all records
without success.
Linear (Serial) Search
Algorithm:
set found to false; set position to –1; set index to 0
while index < number of list. and found is false
if list[index] is equal to search value
found = true
position = index
end if
add 1 to index
end while
return position
Linear (Serial) Search - Tradeoffs
Benefits:
Easy algorithm to understand
Array can be in any order
Disadvantages:
Inefficient (slow): for array of N elements,
examines N/2 elements on average for value in
array, N elements for value not in array
Binary Search
Binary searching better than linear search since all elements the array
must be sorted in order.
– an array of records with integer keys sorted from smallest to largest
(e.g., ID numbers), or
– an array of records with string keys sorted in alphabetical order
(e.g., names).
The techniques of Binary searching
1. Divides the array into three sections:
middle element
elements on one side of the middle element
elements on the other side of the middle element
2. If the middle element is the correct value, done. Otherwise, go to step
1. using only the half of the array that may contain the correct value.
3. Continue steps 1. and 2. until either the value is found or there are no
more elements to examine
Binary Search Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
Binary Search
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
Binary Search
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
11
6 33
3 7 32 53
Search for target = 7
Find midpoint:
3 6 7 11 32 33 53
Start at root:
11
6 33
3 7 32 53
Search for target = 7
Search left subarray:
3 6 7 11 32 33 53
3 7 32 53
Search for target = 7
Find approximate midpoint of subarray:
3 6 7 11 32 33 53
3 7 32 53
Search for target = 7
Search right subarray:
3 6 7 11 32 33 53
3 7 32 53
Introduction to Sorting Algorithms
Sort: arrange values into an order:
Alphabetical
Ascending numeric
Descending numeric
Types of Sorting algorithms are:
Bubble sort
Selection sort
Insertion sort
Bubble Sort
Concept:
Compare 1st two elements
If out of order, exchange them to put in order
Move down one element, compare 2nd and 3rd
elements, exchange if necessary. Continue until
end of array.
Pass through array again, exchanging as
necessary
Repeat until pass made with no exchanges
Bubble Sort - Example
Array numlist3 contains:
17 23 5 11
compare values
17 and 23 – in correct compare values 23 and
order, so no exchange 11 – not in correct order,
so exchange them
compare values 23 and
5 – not in correct order,
so exchange them
Bubble Sort – Example (2)
After first pass, array numlist3 contains:
17 5 11 23
Marked
player
The problem of sorting
Input: sequence a1, a2, …, an of numbers.
Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
L1.40
Example of insertion sort
8 2 4 9 3 6
L1.41
Example of insertion sort
8 2 4 9 3 6
L1.42
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.43
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.44
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.45
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.46
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.47
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.48
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.49
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.50
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
L1.51
Class Activity of insertion sort
47 52 22 99 33
Comparison
Bubble sort is useful for small amounts of
data.
Selection sort can be used when amount of
data is small and swapping is time-
consuming.
Insertion sort is most versatile and the best
when the list is almost sorted.