Binary Search

Binary Search

The binary search technique works only on sorted array.It works by comparing the input element to the middle element of array.The comparison determines whether the element is less than.greater than or equal to middle element.Subsequent steps to be followed are written and explained by the following program.


#include <stdio.h>

int main()
	int val[10] = {10,20,30,40,50,55,68,77,89,90};//Fig 3
	int beg = 0;   //first index of array
	int end = 9;  //end(last index of array)  = n-1 where n is the size of array and is 10 in this case.
	int mid = (beg+end)/2; // finds middle index of array.
	int i;
	int element;
	//printf("enter the element you want search for");
	for(i=0;i<10;i++) //Fig 4
	if(val[mid] == element)	{
	else if(val[mid] < element) {
	beg = mid +1;
	mid = (beg +end)/2;
	end =mid -1;
		mid = (beg +end)/2;
	printf("Location of element is %d",mid);
	return 0;


Fig 3:An Array

Fig 3:An Array

Screen Shot 2014-03-19 at 4.43.35 PM

Fig 4 : Explanation of Binary Search Program

Short URL:
Author: Cusp2207 on July 18, 2014
Category: Algorithms, Computer Science

Leave a Reply

Last articles