Experiment No.10
Experiment No.10
10
Aim: Write a program to implement linear search and binary search.
Objectives:
1) To understand the concept of searching.
2) To implement linear search and binary search.
3) To analyse time complexity of linear and binary search.
Theory:
Linear search
Linear search, also called as sequential search, is a very simple method used for searching an
array for a particular value. It works by comparing the value to be searched with every element of
the array one by one in a sequence until a match is found. Linear search is mostly used to search an
unordered list of elements (array in which data elements are not sorted). For example, if an array A[]
is declared and initialized as,
int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
and the value to be searched is VAL = 7, then searching means to find whether the value ‘7’ is
present in the array or not. If yes, then it returns the position of its occurrence. Here, POS = 3 (index
starting from 0). Figure 10.1 shows the algorithm for linear search.