This is the program to search an element from a sorted array.

Simply enter an sorted array at runtime and then enter the element which you want to search from an array.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],n,i,beg,end,mid,item;
cout<<"length is"<<endl;
cin>>n;
cout<<"enter sorted array"<<endl;
for(i=1;i<=n;i++)
{

cin>>a[i];
}
cout<<"enter what you searching"<<endl;
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=item)
{if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
cout<<"data found at"<<mid+1;
}
else
{
cout<<"not found";
}
getch();

}


Posted by Techno crust On Tuesday, April 29, 2014 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Like us

    as

    Contact Form

    Name

    Email *

    Message *