44 total
By the end of this topic, you should be able to:
understand linear search and binary search
write an algorithm for a linear search
write an algorithm for a binary search
explain the conditions needed for binary search to work
explain how the performance of binary search changes as the number of data items changes
understand insertion sort and bubble sort
write an algorithm for insertion sort
write an algorithm for bubble sort
explain how sorting performance depends on:
understand and use Abstract Data Types (ADTs)
write algorithms to find an item in:
write algorithms to insert an item into:
write algorithms to delete an item from:
understand that a graph is an example of an ADT
describe the key features of a graph
justify why a graph is useful in a given situation
explain how one ADT can be implemented using another ADT or built-in type
describe how these ADTs can be implemented:
compare algorithms using:
use Big O notation for time complexity and space complexity
A linear search is the simplest way to find an item in a list. You start at the first item and check each item one by one until:
This search does not need the list to be sorted. That is why it is useful when data is in any order.
Imagine a list:
[12, 7, 25, 9, 18]
If you want to find 9, a linear search checks:
So the search stops as soon as it finds the value.
A linear search is easy to write and easy to understand. Its weakness is that it can be slow for large lists, because in the worst case it may need to check every single item.
FUNCTION LinearSearch(List, Item) RETURNS INTEGER
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
Index ← 0
Found ← FALSE
WHILE Index < LENGTH(List) AND Found = FALSE
IF List[Index] = Item THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found = TRUE THEN
RETURN Index
ELSE
RETURN -1
ENDIF
ENDFUNCTION
This algorithm returns the index of the item if it is found. It returns -1 if the item is not in the list.
If the list has n items:
For memory use, linear search only needs a few extra variables such as a counter and a flag. So the extra memory is very small and is usually treated as O(1).
A binary search is a faster search method than linear search, but it only works under the right conditions.
Binary search works by repeatedly checking the middle item of the current search area.
There are three possible cases:
Each time, half of the remaining list is ignored.
The list must be sorted before binary search can be used.
If the data is not sorted, binary search can give the wrong answer because the decision to go left or right depends on the values being in order.
For example, in this sorted list:
[16, 19, 21, 27, 36, 42, 55, 67, 76, 89]
If you search for 19:
3619 < 36, so search left half19This is much faster than checking every value from the start.
The middle position is found using:
[ \text{mid} = \left\lfloor \frac{\text{lowerBound} + \text{upperBound}}{2} \right\rfloor ]
This means add the two bounds and divide by 2, rounding down to an integer.
FUNCTION BinarySearch(List, Item) RETURNS INTEGER
DECLARE LowerBound : INTEGER
DECLARE UpperBound : INTEGER
DECLARE Mid : INTEGER
DECLARE Found : BOOLEAN
LowerBound ← 0
UpperBound ← LENGTH(List) - 1
Found ← FALSE
WHILE LowerBound <= UpperBound AND Found = FALSE
Mid ← (LowerBound + UpperBound) DIV 2
IF List[Mid] = Item THEN
Found ← TRUE
ELSE
IF List[Mid] < Item THEN
LowerBound ← Mid + 1
ELSE
UpperBound ← Mid - 1
ENDIF
ENDIF
ENDWHILE
IF Found = TRUE THEN
RETURN Mid
ELSE
RETURN -1
ENDIF
ENDFUNCTION
Binary search is efficient because it cuts the list in half each time.
If the number of items increases, the number of comparisons increases very slowly.
For example:
This is why binary search has O(log n) time complexity.
That means:
A binary search is usually much faster than a linear search on a large sorted list.
A linear search may still be better when:
So binary search is not always the best choice. The algorithm must match the data.
Sign in to view full notes