Introduction To Search Algorithms
Introduction To Search Algorithms
Searching is one of the fundamental classes of computational problems and finds its
applications in almost all non-trivial software. Search deals with finding a known element in
a collection of one or more elements. They goal of search is to confirm if one or more
instances of a search target exists in a collection (usually a list).
In this module, we will consider and analyze two searching algorithms, namely; Linear search
and Binary search.
Linear search is a brute-force algorithm that sequentially compares the target with all elements
in a list and returns the position of the element if found or other output if not found. The
pseudocode below demonstrates a simple linear search algorithm
=====================================================================
endif
endfor
endprocedure
Linear search worst-case complexity is O(n) i.e. the algorithm searches the whole list with
(n-1) comparisons. Average case scenario gives n/2 comparisons and is still O(n). The best
case complexity for linear search is O(1) i.e. constant time.
Binary Search (divide and conquer)
Binary search is a more efficient search approach that works on a sorted list (ascending or
descending). It works by finding the middle element of a list and comparing it with the
search target. It then discards one half of the list based on this comparison and proceeds to
search the remaining half. Below is diagrammatic illustration of binary search1:
The diagram above shows a list of 17 numbers sorted in ascending order. The search target is
7. (Note that the list is zero-indexed)
The algorithm sets out by finding the middle item (using ceil(n/2) = 9 ), where n is the
size of the list (17 in this case). It then compares the element on list[9] = 14 with the target 7.
In this case, 7 is less than 14 so the algorithm discards the upper half of the list, starting from
position 10 to 17 and proceeds with a recursive binary search on the lower list (1 to 8).
The recursive step starts by comparing the middle element in the lower half and with the
target. In this case, the middle element is ceil(n1/2) = 4, where n1 = 8 is the size of the
lower half of the list. The element on list[4] is 6, which is less than 7 so the algorithm discards
the lower half of the sublist (position 1 to 4) and applies a second recursive binary search on
upper sublist (5 to 8)
The final step follows the same procedure and discards elements in position 8 -10 and
discovers the target on position 5.
In the case that the element is not found, the algorithm halts when it cannot further divide
the list into smaller sublists.
1
Diagram courtesy of:
https://en.wikipedia.org/wiki/Binary_search_algorithm#/media/File:Binary_Search_Depiction.svg
Recursive algorithm in pseudo-code:
Input: list of items: list, search item: target, start position: start,
end position: end
=====================================================================
endif
else
return list[mid] // target found
endif
endprocedure
Binary search worst-case complexity is O(log2n) since the algorithm divides the input list by
2 with each recursive call. Average case scenario also gives O(log2n). The best case
complexity for linear search is O(1) i.e. constant time. The critical assumption is that the list
is already sorted. This means that for an unsorted list, the final complexity of binary search
must factor in the cost of sorting.