Searching In C language Binary and Linear Search
Hey, hope you are good. So today we are going on new topic of C programming. We talk about Searching in C, types of searching, Linear Search, Binary Search and their workings with Sample code. So read full article for better understanding.
Also Read: Array In C Language
Search is a technique which allows as to find a given value in a list or a collection at values. If the value is found, the next step is to find out its position.
There are two technique which are used for searching. One is linear search and other binary search.
There are two technique which are used for searching. One is linear search and other binary search.
Linear search
Linear Search is also known as sequential Search because it is search as in sequential way. Linear search works in sequential order. It is difficult and complicated search technique or we can say time consuming technique. This search technique is slower than Binary Search.
Also Read: Control Statements In C - language
Working of Linear Search:-
step 1: Create an array. Enter the data in an array.
step 2: Ask the user to enter the value which to be search. Store that a value in that variable.
step 3: Now Run the logic of linear search. Run a loop from first to last element.
step 4: Compare the value to be searched with the value present at each elements.
step 5: If a match is found stop the searching process and note down the position were data was found display that position.
step 6: If no. match is found in the entire array, print that data is not in the array.
Sample code:-
//Program using Linear Search to search numbers.
#include<conio.h>
#include<stdio.h>
void main()
{
int i;
int n;
int loc=-1;
int a[10];
printf("enter 10 values\n");
for(i=0;i<=9;i=i+1)
{
scanf("%d",&a[i]);
}
printf("enter the number to be searched");
scanf("%d",&n);
for(i=0;i<=9;i=i+1)
{
if(a[i]==n)
{
loc=i;
break;
}
}
if(loc==-1)
{
printf("data not found");
}
else
{
printf("data at %d",loc+1);
}
getch();
}
Binary Search
Binary search is a very efficient searching technique which works extremely fast and is better then linear search. Linear search can work on random values while binary search works only on shorted data.
Also Read: Format Specifier In C Programming
Linear search works on sequential/iterative model. Binary search work on divide and conquer principle.
According to this principle, the number on which searching is to be done are reputedly divided into two groups and after each cycle of searching one group is ignored.
Logic of Binary Search:-
step 1: Calculate mid= (beg+End)/2
step 2: (a). Check whether value at mid is equal to value of 'n' (n=variable).
(b). If yes Stop and note the position.
(c). If No check whether n>value at mid. If Yes then (beg=mid+1). If No
then (end=mid-1).
Sample code:-
//Program using Binary Search to search numbers.
#include<conio.h>
#include<stdio.h>
void main()
{
int i;
int n;
int loc=-1;
int a[10];
int beg=0;
int end=9;
int mid;
printf("enter 10 values\n");
for(i=0;i<=9;i=i+1)
{
scanf("%d",&a[i]);
}
printf("enter the number to be searched");
scanf("%d",&n);
while(beg
{
mid=(beg+end)/2;
if(n==a[mid])
{
loc=mid+1;
break;
}
else if(n>a[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
} }
if(loc==-1)
{
printf("data not found");
}
else
{
printf("data at %d",loc);
}
getch();
}
Also Read: Variables & Datatypes In C Language
Comments
Post a Comment