#include <iostream>
using namespace std;
void sortedarr(int *,int);
int bst(int,int, int, int *);
int main(){
int size;
int a[100];
int data;
cout<<"Enter the size of the array : ";
cin>>size;
cout<<"Enter array elements ";
for(int i = 0; i<size; i++){
cin>>a[i];
}
sortedarr(a,size);
cout<<"Enter the Data : ";
cin>>data;
int b;
b = bst(data,0,size-1,a);
if(b == -1){
cout<<"Not found"<<endl;
}
else{
cout<<"Fount at index no : "<<b+1<<" place"<<endl;
}
}
void sortedarr(int a[],int m){
int k = 1;
int temp,i;
while(k<m){
for(i=0; i<k && k<m; i++ ){
if(i<m){
if(a[i]>a[k]){
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
k=k+1;
i=0;
}
}
int bst(int data,int lb,int ub,int a[]){
int mid;
if((ub+lb)%2 ==0){
mid=(ub+lb)/2;
}
else{
mid = (ub+lb+1)/2;
}
while(lb<=ub){
if(a[mid]==data){
return mid;
}
else if(a[mid]<data){
lb = mid+1;
return bst(data,lb,ub,a);
}
else if(a[mid]>data){
ub = mid-1;
return bst(data,lb,ub,a);
}
}
return -1;
}
Tuesday, April 17, 2012
Subscribe to:
Posts (Atom)