3.4. Searching Algorithms - Advanced
3.4. Searching Algorithms - Advanced
www.pmt.education
Specification:
www.pmt.education
Searching Algorithms
Linear Search
Position 0 1 2 3 4 5
Position 0 1 2 3 4 5
“Oliver” ≠ “Dean”
Check the next position in the array.
www.pmt.education
Position 0 1 2 3 4 5
“Oliver” ≠ “Angelina”
So check the next position in the array
Position 0 1 2 3 4 5
“Oliver” ≠ “Seamus”
So check the next position in the array
Position 0 1 2 3 4 5
“Oliver” = “Oliver”
Hence Oliver is found at position 3 in the array.
Position 0 1 2 3 4 5
“Hannah” ≠ “Dean”
Check the next position in the array.
www.pmt.education
Position 0 1 2 3 4 5
“Hannah” ≠ “Angelina”
So check the next position in the array
Position 0 1 2 3 4 5
“Hannah” ≠ “Seamus”
So check the next position in the array
Position 0 1 2 3 4 5
“Hannah” ≠ “Oliver”
So check the next position in the array
Position 0 1 2 3 4 5
“Hannah” ≠ “Cho”
So check the next position in the array
Position 0 1 2 3 4 5
“Hannah” ≠ “Fred”
Check the next position in the array. There are no more positions in the array, so
Hannah is not contained in the array. When correctly programmed, a linear search
algorithm should not result in an error when trying to locate an item not in the array.
www.pmt.education
Pseudocode for a linear search algorithm could be:
LinearSearch(Target, ArrayofNames)
Boolean Found
Integer Count
Found ← FALSE
Count ← 0
If Found = TRUE
Output Target found at Count
Else
Output Target not found
End if
www.pmt.education
Binary Search
A binary search can be used on any ordered list. If the list
is unordered, the data must be sorted by a sorting
algorithm. A binary search works by looking at the
midpoint of a list and determining if the target is higher or
lower than the midpoint. The time complexity is O(logN)
because the list is halved each search.
Position 0 1 2 3 4 5 6
Where is George?
This is an unordered list, so the first step is to use a sorting algorithm. The data can be
sorted into ascending or descending order, although each will require a slightly different
code.
Position 0 1 2 3 4 5 6
The first step is to take the middle piece of data. To find the midpoint of the data, add
the highest position and the lowest position of the array being considered, and divide by
2. I.e. 0 + 6 = 6, 6/2 = 3. Look at position 3 of the array.
Position 0 1 2 3 4 5 6
“George” ≠ “Ginevra”
www.pmt.education
“George” < “Ginevra” because George is before Ginevra when in alphabetical order.
Your programming language can compare strings to determine whether they are higher
or lower than one another.
Position 0 1 2
Position 0 1 2
“George” ≠ “Fredrick”
“George” > “Fredrick”
Hence, everything before “Fredrick” does not need to be checked.
Position 2
Data George
Position 2
Data George
“George” = “George”
George is found at position 2 of the array.
www.pmt.education
Binary Search Example 2
Position 0 1 2 3 4 5
Where is “Pegasus”?
Position 0 1 2 3 4 5
The first step is to find the midpoint. 0 + 5 = 5, 5/2 = 2.5, there is no position 2.5 in the
array, so an int calculation is performed on it - this removes the decimal part. Hence, we
need to check the data in position 2.
Position 0 1 2 3 4 5
“Pegasus” ≠ “Gus”
“Pegasus” > “Gus”, so only positions 3, 4 and 5 will be considered from now on.
Position 3 4 5
Position 3 4 5
“Pegasus” ≠ “Pascal”
www.pmt.education
“Pegasus” > “Pascal”
Positions 3 and 4 are disregarded.
Position 5
Data Zazu
Position 5
Data Zazu
“Pegasus” ≠ “Zazu”
“Pegasus” > “Zazu”
There is no more data to check; Pegasus isn’t in the array.
www.pmt.education
A binary search can be conducted in many different ways. Here is pseudocode for one
solution:
BinarySearch(Target, ArrayofNames)
Integer TopPointer
Integer BottomPointer
Integer Midpoint
Boolean Found
Found ← FALSE
BottomPointer ← 0
TopPointer ← ArrayofNames Count - 1
If Found = TRUE
Output Target found at Midpoint
Else
Output Target not found
End if
www.pmt.education
Binary Tree Search
The first stage in a binary tree search is to put the list into
a binary tree.
www.pmt.education
“Alan” ≠ “Georg”
“Alan” < “Georg”
Therefore only items left of the root will be considered further.
“Alan” ≠ “Ada”
“Alan” > “Ada”
Hence only nodes right of Ada will be further considered.
www.pmt.education
“Alan” = “Alan”
Alan is in the tree.
www.pmt.education