You are on page 1of 3

1

BINARY SEARCH
Binary search method is very fast and efficient. This method requires that the list of elements must be in sorted ordered. In this method, to search an element we compare it with the element present at the center of the list. If it matches then the search is successful. Otherwise, the list is divided into two halves: one from 0th element to the center element 1st (half) and another from center to the last element 2nd (second half). As a result all the elements in first half are smaller then the center element, where as all the elements in second half are greater than the center element. The searching will now proceed in either of the two halves depending upon whether the element is greater or smaller than the center element. If the element is smaller than the center element then the searching will be done in the first half, otherwise in the second half. Same process of comparing the required element with the center element and if not then dividing the elements into two halves is repeated for the first half or second half. This procedure is repeated till the element is found or the division of half parts gives one element. The advantage of the binary search method is that, in each iteration it reduces the number of elements to be searched from n to n/2.While in other hand, linear search method checks sequentially for every element, which makes it inefficient. The disadvantage of binary search is that it works only on sorted lists. So when searching is to be performed on unsorted list then linear search is the best option. To understand the Binary Search we can take the example as, suppose one wants to find the location of some name in a telephone directory. Obviously, one does not perform a linear search. Rather, one opens the directory in the middle of determine which half contains the name being sought. Then one opens that quarter in the middle to determine which part of the directory contains the name. And so on. Algorithm for Binary Search:-BINARY (DATA, LB, UB, ITEM, LOC) Here DATA is sorted array with lower bound LB and upper bound UB and ITEM is a given item of information. The variable BEG, END and MID denote, respectively, the beginning, end and middle locations of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL Complied by: -- Subrat Kumar Dash

2 1. [Initialize segment variable] Set BEG:=LB, END :=UB and MID=INT((BEG+END)/2). 2. Repeat steps 3 and 4 while BEG<=END and DATA[MID]ITEM 3. If ITEM<DATA[MID], then: Set END:=MID-1 Else: Set BEG:=MID+1 [End of If structure] 4. Set MID:=INT((BEG+END)/2) 5. If DATA[MID]=ITEM, then Set LOC:=MID Else: Set LOC:=NULL [End of If Structure] 6. Exit PROGRAM OF BINARY SEARCH #include<stdio.h> #include<conio.h> void main() { int n,j,a[100],i,max,min,r,n1,t,f; clrscr(); printf("\nENTER THE NUMBER OF ELEMNTS IN THE ARRAY :-- "); scanf("%d",&n); printf("\nENTER THE ELEMENTS IN THE SORTED ORDER :-- "); for(j=0;j<n;j++) { scanf("%d",&a[j]); } printf("\nENTER THE ELEMENT THAT YOU WANT TO SEARCH :-- "); scanf("%d",&n1); min=0; max=n; r=(max+min)/2; do { if(n1==a[r]) { f=1; break; } if(r==max || r==min) { break; } Complied by: -- Subrat Kumar Dash

3 if(n1>a[r]) { min=r+1; } if(n1<a[r]) { max=r-1; } r=(max+min)/2; }while(r>=0); if(f==1) printf("\nTHE %d IS FOUND AND THE INDEX POSITION IS %d",n1,r); else printf("\nTHE %d IS NOT FOUND",n1); }

Complied by: -- Subrat Kumar Dash

You might also like