Binary search in Data Structures
The binary search in data structures is yet another simple method.
However in the binary search, the algorithm expect the list to be sorted you can apply any one of the sorting methods before using the binary search algorithm.
In binary search the given list is devided into two equal half’s. The given key is compared with the middle element of the list now three situations may occur.
The middle element matches with the key the search will end peacefully here.
The middle element is greater than the key then the value which we are searhing is possibly in the first half of the list.
The middle element is lower than the key. Then the value which were searching is possibly in the second half of the list
Now the list is searched either in the first half is in the second half of got by the result of conditions (b) and (c) given above. This search fails because the key does not exist
Mid= 0+9/2 = 4.5
# include <stdio.h>
# include <conio.h>
printf(“Enter the total number of elements \n”);
printf(“Enter the element in ascending order \n”);
printf(“Enter the search element \n”);
p=binary search(a,n,key); /* Functional */
printf(“search is successful \n”);
printf(“The element found at position %d”,p+1);
printf(“search is unsuccessful search \n”);
printf (“search is unsuccessful search \n”);
int binary search(int a,int n, int key)
int left, right, mid;
Each step at the algorithm divides the list divided into two parts and the search continues in one of them and the other is discarded the search requires at mast k steps where 2k > =N which results k=log2N thus the running time c both average and worst case of binary search is proportional