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...

Structure Of C Language Program Btech BCA Notes

  Structure Of C Language Program Btech BCA Notes Before begin with topic I will share some important term and sequence of coding in c programming language. Without them code of C is useless and nothing without them. So, they are stdio.h and conio.h header files. What is Stdio.h Header file In C Programming? Stdio.h means Standard input output, which manages the fundamental operations required to run a program. Example is creation of variables assigning them memory etc. What is conio.h Header file In C Programming? Conio.h means console input output, which handles the entire input output operation during the course of execution of the program. you may also like our previous post , first post of C language  Beginner our view on C programming Language    also read it . What is the Structure of C language program? What is the Structure of C language program? Generally there are five structure of c language program. We use them in every program, Without the ...

Whatsapp chats and Status tricks 2021

whatsapp chatting and status trick Today we will talk about some secret options of WhatsApp which you should know. We will talk about how we can see someone's Whatsapp status without anyone knowing it. And at the same time, we will also talk about how to see someone's message, without getting blue ticked in his phone. To know about all this, read this entire article carefully.  1.  Let's know about Whatsapp In the whole world, whatsapp is the only messenger application to be used the most. WhatsApp is a free application that anyone can use for absolutely free. We do not see any advertisement like other social media applications, meaning It does not earn any money from its own app. WhatsApp was created by Brian Acton and Jan Koum in 2009. After 2014 facebook bought whatsapp, since then it is now owned by facebook. WhatsApp is now controlled and maintained by facebook. As you may know, Facebook is a multinational company. There are many products of Facebook which we use every...