Sample Output |
Binary search with simple Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Author : Shohanur Rahaman | |
#include<stdio.h> | |
int main() | |
{ | |
int i,j,sv,pos,tmp; | |
int data[]={8,-5,-9,45,0,88,36,40,90,5,6}; | |
int size=11; | |
// sort array in ascending order | |
for(i=0;i<size;i++){ | |
for(j=i;j<size;j++){ | |
if(data[i]>=data[j]){ | |
tmp=data[i]; | |
data[i]=data[j]; | |
data[j]=tmp; | |
} | |
} | |
} | |
// print sorted array | |
for(i=0;i<size;i++) | |
printf("%d ",data[i]); | |
while(1){ | |
printf("\nEnter searching value : "); | |
scanf("%d",&sv); | |
// searching Binary Search | |
int lo=0,hi=size-1,mid=0,check=0; | |
while(lo<=hi){ | |
mid=(lo+hi)/2; | |
if(data[mid]==sv){ | |
check=1; | |
break; | |
} | |
else if(data[mid]<sv) | |
lo=mid+1; | |
else | |
hi=mid-1; | |
} | |
if(check==1) | |
printf("\n%d Found\n",sv); | |
else | |
printf(" \n%d Not Found\n",sv); | |
} | |
return 0; | |
} | |
Binary Search Without Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Author : Shohanur Rahaman | |
#include<stdio.h> | |
int main() | |
{ | |
int lo,hi,mid,sv,ck,i,size=0; | |
int data[100]; | |
printf("Enter Size of Array: "); | |
scanf("%d",&size); | |
printf("Enter Number of Element Of The Array In Ascending Order : "); | |
for(i=0;i<size;i++) | |
scanf("%d",&data[i]); | |
// seraching | |
printf("Enter The Number To be Search : "); | |
scanf("%d",&sv); | |
lo=0,hi=size-1,mid=0,ck=0; | |
while(lo<=hi){ | |
mid=(lo+hi)/2; | |
if(data[mid]==sv){ | |
ck=1; | |
break; | |
} | |
else if(data[mid]<sv) | |
lo=mid+1; | |
else | |
hi=mid-1; | |
} | |
if(ck==1) | |
printf("%d Found\n",sv); | |
else | |
printf("%d Not Found\n",sv); | |
return 0; | |
} |