#include<
iostream>
#include<conio.h
>
using namespace std;
int main()
{
int n,num,first=0,middle,last;
cout<<"Enter the size of your array : ";
cin>>n;
int *arr=new int[n];
cout<<"Enter element of array\n";
for(int i=0;i<
n;i++)
{
cin>>arr[i];
}
cout<<"\nElement of array\n";
for(int i=0;i<
n;i++)
{
cout<<arr[i]<<"\t";
}
cout<<"\nEnter number to search : ";
cin>>num;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<
num)
{
first=middle+1;
}
else if(arr[middle]==num)
{
cout<<num
<<" is found at "<<middle+1<<" possition";
break;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
if(first>last)
{
cout<<num
<<" is not found in the array list.";
}
getch();
return 0;
}
No comments:
Post a Comment