Professional Documents
Culture Documents
Experiment No. 13
Year/Branch SE AI&DS
Roll No.
UIN 201A069
Aim: To Implement Binary Search.
Theory:
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of
the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.
Example :
Rizvi College of Engineering, Mumbai
Algorithm:
1. Start
2. Int low, mid, high
3. Low 0
4. High = size of array I
5. Run till low<— high
6. Mid = (low + high)/2
7. If array[mid] is equal to element return mid (that means mid is value we are
searching)
8. If array[mid] < element then low = mid + I
9. If array[mid] > element then high — mid I
10.Else element is not present
11. End
Program:
Rizvi College of Engineering, Mumbai
#include<stdio.h>
If(arr[mid]<element){
low = mid+l;
int arr[100];
printf("Enter the size of array:
scanf("%d", &size);
printf("Enter the array:
for(i=e;i<size;i++)
&arr[i] );
A hinnrysearch algorithm is a widely used algorithm in the computat ional domnin_ It is fast
and accuratcscarch algorithm dual can work well on both hig and small dalaset_ A binary search
algorithm is a. simple and reliable algorithm to implement, only condition is dataset must bC
attangcd in order.