Header Ads

Header ADS

Program for Binary Search

Introduction

Hello friends, In this post, we will learn how to write a simple program in C for Binary Search.
This tutorial is for those people who want to learn programming in C and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.
Here we are calling two standard library functions. Whenever we call the library functions we must write their prototype before making the call.This helps the compiler in checking whether the values being passed and returned are as per the prototype declaration.But since we don’t define the library functions (we merely call them) we may not know the prototypes of library functions. Hence when the library of functions is provided a set of ‘.h’ files is also provided.These files contain the prototypes of library functions. But why multiple files? Because the library functions are divided into different groups and one file is provided for each group. For example, prototypes of all input/output functions are provided in the file ‘stdio.h’, prototypes of all mathematical functions are provided in the file ‘math.h’, etc.
On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. You can even open this file and look at the prototypes.

Program Briefing

First, we call the standard library function 'stdio.h' because prototypes of all input/output functions are provided in the file ‘stdio.h’. Then we call the standard library function 'conio.h' because On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. Then comes the program's main body. We declare six integer variables i.e; i,n,key,low,mid,high and a integer array 'a'. Then we clear the screen. Then we ask for the number of total values and store it in 'n'. Then we ask for the values in ascending order and store them in array 'a'. Then we ask for the key to search and store it in variable 'key'. We set variable low at '0' and high at the total number of values. then we find its mid and compare the key value with mid. If  key is less than mid then we set the high value to (mid-1) and if key is greater than mid then we set the low value to (mid+1). We repeat the above process till we get the value of mid equal to value of key and display its position.

Program

//Program for Binary Search :

 #include<stdio.h>
 #include<conio.h>

 void main()
 {
   int a[100],i,n,key,low,mid,high;
   clrscr();
   printf("Enter the size of array\n");
   scanf("%d",&n);
   printf("\nEnter the Values in ascending order :\n");

   for(i=0;i<n;i++)
   {
     scanf("%d",&a[i]);
   }

   printf("\nEnter the key\n");
   scanf("%d",&key);
   low=0;
   high=n-1;
   mid=(low+high)/2;

   while(low<=high && key!=a[mid])
   {
     if(key<a[mid])
     high=mid-1;
     else
     low=mid+1;
     mid=(low+high)/2;
   }

   if(key==a[mid])
   printf("\n\nKey is found at %d.",mid+1);
   else
   printf("\n\nKey is not found.");

   getch();
 }



No comments

Powered by Blogger.