Binary Search






SOURCE CODE



#include”iostream.h”
#include”conio.h”

void binsearch(int ar[],int size,int ele)
{
int lb=0,ub=size-1,mid;
for(;lb < =ub;) { mid=(lb+ub)/2; if(ar[mid]==ele) { cout < < "\n SEARCH SUCCESSFULL"; break; } else if(ar[mid] < lb="mid+1;" ub="mid-1;" i="0;i" j="i+1;j"> ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}

void display(int ar[],int size)
{
cout < < "\n The Elements In Sorted Order Is \n"; for(int i=0;i < i="0;i"> > ar[i];
}
void main()
{
clrscr();
int size;
cout < < "Enter The Number Of Elements : "; cin > > size;
int *ar=new int(size);
cout < < "\n Enter The Elements Of The Array \n"; input(ar,size); sort(ar,size); int ele; display(ar,size); cout < < "\n Enter The Element To Be Found: "; cin > > ele;
binsearch(ar,size,ele);
getch();
}




OUTPUT


Enter The Number Of Elements : 5
Enter The Elements Of The Array
2
4
6
3
1

The Elements In Sorted Order Is
1
2
3
4
6
Enter The Element To Be Found: 1
SEARCH SUCCESSFULL