Binary Search 1

Description:           This program implements the binary search algorithm.

 
Primary Inputs:    An array of 10 numbers (sorted in increasing order), and another number to be searched.
Primary Output:    Gives the location of the number in the array(if found).
Platform Used:      Turbo C++ version 3.0, Borland International Inc.
 


/* This program implements the Binary
* Search Algorithm on an input sorted array*/

#include
#include
void main()
{
/*array a will hold the sorted numbers, n will hold the number to be searched*/
int a[10],n,mid,low=0,high=9;
clrscr();
/* Calculate middle location of the array*/
/* integer range may exceed and cause problem */
mid=(low+high)/2;
printf("\n Enter the array elements :");
for(low=0;low<10;low++) scanf("%d",&a[i]); low=0; printf("\n Enter the no. to be searched :"); scanf("%d",&n); while(low<=high) { if(a[mid]==n) { printf("\n The no. is found at the location : %d",mid+1); break; } else if(a[mid]>n)
high=mid-1;
else if(a[mid]high)
printf("\n The no. is not found in the array");
getch();
}

 

Rate this post

One comment on “Binary Search

  1. Reply zi xiu tang Jul 20,2013 1:01 pm

    Appreciation to my father who stated to me concerning this website, this web site is actually amazing.

Leave a Reply