Searching and Sorting Arrays
Searching and Sorting Arrays
Science II
SEARCHING AND SORTING ARRAYS
Searching Algorithms
• A search algorithm is a method of finding a specific item
that is stored in an array or other collection of data.
• Two search algorithms
• The linear search
• The binary search
The Linear Search Algorithm
• Also called a sequential search
• Uses a loop to try to find a specific item in an array. It compares the
item with the contents of the 1st array element, then the 2nd array
element, then the 3rd array element, and so on.
• When the item is found the linear search algorithm stops.
• If the item is not in the array, the linear search algorithm will search
to the end of the array.
Pseudocode for the Linear
Search Algorithm
Set found to FALSE
Set index to 0
Set position to -1
While found is FALSE and index is less than the array size
If array[index] equals search value then
set found to TRUE
set position to index
Else
increment index
End If
End While
Return position
Linear Search Algorithm Code
(p. 453)
Linear Search Algorithm Code (Alternative Methods)
Linear Search Example
3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1. When the binary search algorithm completes, what will be the values of the following?
a) first
b) middle
c) last
answers
1. (a) first = 12, (b) middle = 12, (c) last = 13
2. 4
Student Practice Problem
search for value = 63
5 8 12 17 21 29 30 36 39 50 52 63 68 72 79 81 91 95
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1. When the binary search algorithm completes, what will be the values of the following?
a) first
b) middle
c) last
answers
1. (a) first = 11, (b) middle = 11, (c) last = 12
2. 4
The Binary Search Algorithm
Pseudocode
Set position to -1
Set found to FALSE
Set first to 0
Set last to ARRAY_SIZE – 1
While Not found AND first <= last first > last implies that the item is not in the array
Set middle to (first + last) / 2
If array[middle] equals value
set found to TRUE
set position to middle
Else If array[middle] is less than value
set first to middle + 1
Else
set last to middle – 1
End If
End While
Return position
The Binary Search Algorithm
Code (p. 457)
The Binary Search Algorithm (Alternative method)
The Binary Search Example
1. How many times will the bubble sort algorithm pass through the entire array?
answer
7
2. What will the array look like after the 3rd pass?
answer
1 4 2 0 3 5 6 8 9
Bubble Sort Pseudocode
Do
Set swap flag to false.
For index = 0 TO the next-to-last subscript
If array[index ] is greater than array[index + 1]
Swap the contents of array[index ] and array[index
+ 1].
Set swap flag to true.
End If.
End For.
While any elements have been swapped.
Bubble Sort Code
Bubble Sort Example
1 4 8 3 2
1 2 8 3 4
1 2 3 8 4
1 2 3 4 8
The Selection Sort
9 4 0 1 8 7 6 2 3
1. How many array-element swaps will the selection sort algorithm make to sort this array in ascending order?
answer
7
2. What will the array look like after the 4th array-element swap?
answer
0 1 2 3 8 7 6 9 4
Selection Sort Pseudocode
For count = 0 TO next-to-last subscript
Set minimum_value to array[count ].
For index = (count + 1) TO the last subscript
If array[index] is less than minimum_value
Set minimum_value to array[index].
Set minimum_index to index.
End If.
End For.
If minimum_value is NOT array[count]
Set array[minimum_index] to array[count ].
Set array[count ] to minimum_value .
End If.
End For.
Selection Sort Code
Selection Sort Example