Sequential Search: 7.10 Write A Programe That Initialize An Array .It
Sequential Search: 7.10 Write A Programe That Initialize An Array .It
7.10
Write a programe that
initialize an array .It
input a value frome
user search the number
in the array .
#includ<iostream.h>
#include<conio.h>
Void main()
{ i
nt arr[10]={10,20,30,40,50,60,70,80,90,100}
;
int i ,n ,loc=-1;
assigement c.lnk
clrscr();
cout<<" enter value to find ";
cin>>n;
if(i=o;i<10;i++)
if(arr[i]==n)
loc=i;
if(loc==-1)
cout<<"value not found in the array";
else cout
<<" value fiund at index" <<loc;
getch(); }
Binary search Binery
search is quickr method of searching for
values in the array .Binery search is quicker
method but it can only search sorted
array.It cannot apply on un sorted array .
It locate the middle element of the arry
compare with search element.
c If they are equal ,search is successful
and index of middle element returned.
If they are not equal, it reduced the search
to half of the elemenn . If
the seach number is less then middle
element , it search the first half of the array
Program7.11
Write a prograne that initalize an array of
ten integers .It inputr integers from the
user and serach the value in the array using
the binery search.
include<<iostream.h>
#include<<conio.h>
voide main()
{ clrs
cr() ; int
array [10]=
{10,20,30,40,50,60,70,80,90,100}
int n,i,mid, start, end, loc;
loc=-1;
start=0;
end=9;
cout<<"enter any number to fiend ";
cin>>n;
while(start<=end)
{
mid=(start+end)/2
If(arr[mid]==n)
{
loc=mid;
break;
}
else if (n,arr[mid])
end=mid-1 ;
else
start=mid+1
}
if (loc==-1)
cout<<n>>"not found"<<endl;
else
cout<<n<<"found at index"<<loc<<endl;
getch();
}