0% found this document useful (0 votes)
13 views

Introduction To Search Algorithms

This document introduces and compares two search algorithms: linear search and binary search. Linear search sequentially compares the target to all elements in a list, having a worst-case complexity of O(n). Binary search works on a sorted list, recursively dividing the list in half and discarding portions that cannot contain the target, resulting in a more efficient worst-case complexity of O(logn). Pseudocode is provided to demonstrate how each algorithm works.

Uploaded by

Kutemwa Mithi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Introduction To Search Algorithms

This document introduces and compares two search algorithms: linear search and binary search. Linear search sequentially compares the target to all elements in a list, having a worst-case complexity of O(n). Binary search works on a sorted list, recursively dividing the list in half and discarding portions that cannot contain the target, resulting in a more efficient worst-case complexity of O(logn). Pseudocode is provided to demonstrate how each algorithm works.

Uploaded by

Kutemwa Mithi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 (sequential) 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

Input: list of items: list, search item: target

Output: target position if found, -1 if target not found

=====================================================================

procedure linear_search(list, target)


index := 0
for(each item in list)
if (list[index] = target) then
return index // item found

endif

endfor

return -1 //not found

endprocedure

Linear search complexity:

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

Output: target position if found, -1 if target not found

=====================================================================

procedure binary_search(list, target, start, end)


if(end < start) then
return -1 // target not found

endif

mid := (start + end)/2 // calculate middle position

if(list[mid] > target) then


// do a recursive call on the lower half
return binary_search(list, target, start, mid-1)

else if(list[mid] < target) then


// do a recursive call on the upper half
return binary_search(list, target, mid+1, end)

else
return list[mid] // target found

endif

endprocedure

Binary search complexity:

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.

You might also like