0% found this document useful (0 votes)
19 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)
19 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
You are on page 1/ 2

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy