Skip to main content

Searching In C language 'Binary search' and 'Linear Search'

 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.


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.

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.

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.


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();
}


We will going on series of C Programming language of all topics. So, you can follow us for more updates.

Comments

Popular posts from this blog

Control Statements in c language

So, Today we are going on new topic name control statements in C language. In which we read Looping(For, While and Do While), Jumping(Break, goto and Continue) and Decision Control statements(Switch, If, If Else and  If else if ladder)  in C. Before you read this article May you also like our previous posts :-    Structure Of C Language Program   Variables & Datatypes In C Language    Format Specifier In C Programming  .   It may help you to understanding it. A Statement is the smallest unit that is a complete instruction in itself statements contains expression and usually end with a semicolon. Statements are of two Types:- Types of Statements in C  1. Sequential Statement   2. Control Statement Sequential Statements are statements which are executed by one in sequential manner. Control Statements are the statements which alter the flow of execution and provide better control to the programme. On the flow of execution. The...

Array in C Language Btech Bca Notes

 Array in C Language Btech Bca Notes An array is a data form that can hold several values all of the same type. An array is a collection of variable of the same type that are referral to by a common name. Also Read:   Beginner our view on C programming Language  . Array are vital for most programming language. They are collection of variables, which we call elements. Each of the element in an array is stored sequentially in the computeric memory.  This making it easy to  m anipulate them and navigate among them. Since all the elements in an array are of same type, array allows us to represent a group of similar elements as an ordered  sequence  and work on them as a whole (grouping together related data). Array organize data in such a way that it can be easily sorted. To create an array, we use a declaration statement. An array declaration should indicate three things: 1. The type of value to be stored in each element. 2. The name of the array. 3. The ...

Format Specifier In C Programming Btech BCA Notes

   Format Specifier In C Programming Btech BCA Notes Welcome, Hope you are doing well. Today we are talking about Foramat specifier in C language. We can also say Type Specifier In C.  So, Today we are read formate specifier as well as printf and scanf Formats in different situations and different conditions. Before you read this article May you also like our previous posts :-    Beginner our view on C programming Language     Structure Of C Language Program   Variables & Datatypes In C Language  . It may help you to boost Your Information. In C Programming language, Types specifier is a special piece of code which is used during the input and output. Type specifier inform the compiler about the type of data which will be entered or displayed during an input output operation. Input mans reading data from keyword while output means displaying data either on screen or a printer. In c – Language every datatype has its own type specifier...