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. They are useful to write better and complex program. Control S

Beginner our view on C programming Language

  Beginner our view on C programming Language   Beginner ourview on C programming Language History about C programming Language. The founder or we can say developer of C programming language was Dennis Ritchie. He was found in  Bell labs USA. The Birth of C language is originally on 1972. After the time C is widely popular all around the world. C is also used in to develop many OS system some are Linux and Windows. Now days C is still widely used or popular language in Programming world. Here, we have a question is What is a Program ?  A Program is a set of instruction which on   execution by a machine produces a specific result. A machine needs set of instructions to perform an operation. Those set of instruction are contained in a program. What is program? What is Programming languages ? What Is a Programming Languages? Meaning- A programming language allows a programmer to develop instructions which can be executed by a machine. A programming language acts as an intermediate bet

What is Blogger? How we create a account on it.

What is Blogger ? How we create a account on it. Welcome to blog. Here I am talking about blog on blogger.com. Hope you guys like it, Thanks. Here, I will talk about Ø What is blog. Ø About blogger. Ø How to create an account on blogger. Ø Can we earn money to blog. What is Blog Blog is a kind of platform, where you can write your thinking about that particular topic. Also you can say blog is article which we write through blogger.com and WordPress, etc. After writing the blog we go to publish in internet. If we write on a unique content hope traffic will came on our blog. You should write a blog on different topics like - Games, Technical, programming, Knowledge, education, and many more you like. What is blogger.com     Blogger is website that manages by Google. Google bought blogger from Pyra labs in 2003.Blogger is free for all, it give space to our passion in his platform. Around the world millions of peoples are used blogger daily and post something every