0% found this document useful (0 votes)
26 views2 pages

Binary Search

code on binary search through c

Uploaded by

niyije9563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Binary Search

code on binary search through c

Uploaded by

niyije9563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment no 9

// 9)Searching of element through binary search

#include <stdio.h>
int binarySearch(int arr[], int size, int element)
{
int low, mid, high;
low=0;
high=size-1;
while (low<=high) {
mid = (low+high)/2;
if (arr[mid] == element) {
return mid;
}
if (arr[mid] < element) {
low = mid+1;
} else {
high = mid-1;
}
}
return -1 ;
}

int main()
{
int element;
int arr[] = {1,2,4,56,64,78,89,123,156,244,299,300};
int size = sizeof(arr)/sizeof(int);
printf("Enter the element you want to search\n");
scanf("%d",&element);
int SearchIndex = binarySearch(arr,size,element);
if (SearchIndex == -1) {
printf("The element %d is not present \n",element);
} else {
printf("The element %d was found at index %d\n", element, SearchIndex);
}

return 0;
}
OUTPUT:

You might also like