0% found this document useful (0 votes)
5 views

Binary Search

Uploaded by

RAMSWAMI BANSODE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Binary Search

Uploaded by

RAMSWAMI BANSODE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

//Program on Binary Search

#include<stdio.h>

int main() {

int a[20],i,n,j,k,key,low,high,mid;

printf("Enter array Size: ");

scanf("%d", &n);

printf("Enter array elements: \n");

for(i=0; i<n; i++){

scanf("%d", &a[i]);

for(i=0; i<n; i++){

for(j=i+1; j<n; j++){

if(a[i]>a[j]){

k = a[i];

a[i] = a[j];

a[j] = k;

}}}

printf("Enter the key element: \n");

scanf("%d", &key);

low=0;

high=n-1;
while(high>= low){

mid = (low + high)/2;

if(key == a[mid]){

break;

} else{

if(key > a[mid]){

low = mid + 1;

} else{

high = mid - 1;

}} }

if(key == a[mid]){

printf("The key element is found at %d location", mid+1);

} else {

printf("The key element is not found!");

return 0;

You might also like