Chapter 2 - Searching: Linear Search
Chapter 2 - Searching: Linear Search
Linear Search
It is a simplest method to find an element in the list. This method is also called as sequential search. In this
search, search element is compared with each element from the list starting from first element. Search process
continues in sequence till list element matches with search element or search process reaches to the comparison
of last element from the list with search element.
Linear search on sorted array:-On sorted array search takes place till list element matches with search element
or search finds a list element greater than search element.
10 ! = 30
Index = Index + 1
20 ! = 30
Index = Index + 1
30 = 30
Number is found
Advantages:-
• Simple method of searching
• Works on unsorted as well as sorted list.
• Easy implementation
Disadvantages:-
• It takes more time with respect to the number of comparisons if element list is large in size.
Algorithm :
Step 1 : Start.
Step 2 : Declare variables array-a[5], search element-se, index position-i, flag=0
Step 3 : Accept values of an array-a and search element-se, initialize i=0.
Step 4 : Repeat step 5 till i < 5 (last location of an array can be N)
Step 5 : [Compare search element with array elements]
If search element=array element [if (se= =a[i])] then
Set flag=1
Then display message as ‘element found’ and stop search process.
Otherwise
Increment index position (i) by one and go to step 4
step 6: If flag = = 0 then
Display message as ‘element not found’
Step 7 : Stop.
Program :
# include <stdio.h>
# include <conio.h>
void main ()
{
int a[5] = {10, 20, 30, 40, 50},se,i,flag=0;
clrscr( );
printf (“Enter search element”);
scanf (“%d”,&se);
for (i = 0; i <5; i ++)
{
if (a[i] = = se)
{
flag=1;
printf(“Element found”);
break;
}
}
if (flag= =0)
printf (“Number not found”);
getch( );
}
Binary Search
Binary search can be performed only on sorted array. In this method, first calculate mid position in an array
and compare the mid position element with the search element. If a match is found then stop the search process
otherwise divide the input list into two parts. First part contains all the numbers less than mid element and
second part contains all the numbers greater than mid element.
The binary search performs the comparison till the element is found or division of list gives one element to
perform comparison.
Example:
Input list : 0, 1, 2, 3, 10, 11, 15, 20, 46, 72 // If the elements are not in sorted order then first perform sorting.
Search element (se) : 15
Input list is implemented with an array of 10 elements so lower index is 0 and upper index is size-1=9.
Iteration 1:
lower = 0 , upper = 9
mid = (lower + upper) / 2= (0 + 9)/2= 4.5 // In case of decimal point answer, consider only integer number.
mid ! = 15
mid<se; lower = mid + 1=5
Iteration 2:
lower = 5 , upper = 9
mid = (Lower + Upper) / 2= (5 + 9) / 2= 7
mid ! = 15
mid>se; upper = mid -1=6
Iteration 3:
lower = 5 , upper = 6
mid = (Lower + Upper) / 2= (5 + 6) / 2= 5.5
mid ! = 15
mid<se; lower = mid + 1=6
Iteration 4:
Lower = 6 , upper = 6
mid = (Lower + Upper) / 2= (6 + 6) / 2= 6
mid = 15
Number is found
Algorithm :
Step 1 : Start
Step 2 : Declare variables array-a[10], search element-se, lower,upper,mid, flag.
Step 3 : Accept values of an array-a, search element-se and initialize lower=0,upper=9,flag=0.
Step 4 : Repeat step 5 & 6 till lower is less than equal to upper
Step 5 : Calculate mid position with the formula as mid = (lower + upper) / 2.
Step 6 : Perform comparison:
If a [mid] = se i.e if mid position element is equal to search element then
Set flag=1 and display message as ‘Element found’ and stop the search process.
Otherwise perform
If a [mid] > se i.e. if mid position element is greater than se then
Set upper = mid -1 and go to step 4.
otherwise
Set lower = mid +1 and go to step 4.
Step 7 : If flag= 0 then
Display message as ‘Element not found’ .
Step 8 : Stop.
Program :
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a[10]={0,1,2,3,10,11,15,20,46,72},se, mid, lower=0, upper=9,flag = 0;
printf (“Enter number to be searched”);
scanf(“%d”, &se);
while (lower< = upper)
{
mid = (lower + upper) / 2;
if ( a[mid] = = se)
{
flag = 1;
printf(“Number found”);
break;
}
else
{
if (a [mid] > se)
upper = mid –l;
else
lower = mid + 1;
}
}
if (flag = = 0)
print f (“Number is not found”);
getch( );
}
Advantages:-
• Fast execution i.e. it takes less number of comparisons for searching an element from a large list.
Disadvantage:-
• List has to be in sorted order i.e binary search works only on sorted array.