Binary Search for Ascending order Numbers :
/*
This is a program for binary serach
Date : 21 April 2014
Time : 11:10 PM
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]= {41,50,65,95,100}; //ascending order
int no,beg=0,end=4,mid,f=0;
printf("\nEnter a Number to Search : ");
scanf("%d",&no);
//following method is only for ascending order
while(beg<end){
mid = (beg+end)/2;
if(a[mid]==no){
f=1;
break;
}
else if(a[mid]<no){
beg=mid+1;
}
else{
end=mid-1;
}
}
if(f==0){
printf("\nSearch Unsuccessful");
}
else{
printf("\nSearch Successful");
}
getch();
return 0;
}
Download Source File : Binary_search.c
if you wanna same but with descending order ..so just need to change a condition in
else if(a[mid]>no){
beg=mid+1;
}
else{
end=mid-1;
}
Download Source file :Binary_search_des.c
/*
This is a program for binary serach
Date : 21 April 2014
Time : 11:10 PM
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]= {41,50,65,95,100}; //ascending order
int no,beg=0,end=4,mid,f=0;
printf("\nEnter a Number to Search : ");
scanf("%d",&no);
//following method is only for ascending order
while(beg<end){
mid = (beg+end)/2;
if(a[mid]==no){
f=1;
break;
}
else if(a[mid]<no){
beg=mid+1;
}
else{
end=mid-1;
}
}
if(f==0){
printf("\nSearch Unsuccessful");
}
else{
printf("\nSearch Successful");
}
getch();
return 0;
}
Download Source File : Binary_search.c
if you wanna same but with descending order ..so just need to change a condition in
else if(a[mid]>no){
beg=mid+1;
}
else{
end=mid-1;
}
Download Source file :Binary_search_des.c
Program for Binary search (Ascending/Descending order Numbers)
Reviewed by Unknown
on
11:11
Rating:
No comments: