You are on page 1of 6

Rizvi College of Engineering, Mumbai

RIZVI EDUCATION SOCIETY'S

Rizvi College of Engineering


•Approved by AICTE • Recognized by DTE •Affiliated to University of Mumbai •Accredited B+ by NAAC
New Rizvi Educational Complex, OffCarter Rd, Bandra West, Mumbai-400050

Experiment No. 13

Title Implement Binary Search

Name Cezzane khan

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>

int binarySearch(int arr[ ] , int size, int element){ int


low, mid, high; low e; high = size-1;
while(low<=high){ mid -
(low + high)/ 2 if(arr[mid]
- element){ return mid;

If(arr[mid]<element){
low = mid+l;

else{ high - mid -1;


return
1;
int main int
element, size, i;
Rizvi College of Engineering, Mumbai

int arr[100];
printf("Enter the size of array:
scanf("%d", &size);
printf("Enter the array:
for(i=e;i<size;i++)
&arr[i] );

printf("Enter the element to be search: scanf("%d'%


&element);

int searchlndex binarySearch(arr, size, element); printf("\nThe element %d


was found at index %d , element, searchlndex) ; return 0;
Output:
Conclusion:

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.

You might also like