0% found this document useful (0 votes)
35 views

Sequential Search: 7.10 Write A Programe That Initialize An Array .It

Sequential search is a linear search that searches through an array element by element to find a target value. It checks each element against the target and stops when a match is found. Binary search is a faster search for sorted arrays that locates the middle element, compares it to the target, and recursively searches half of the array. It returns the index if a match is found or indicates the target was not found. Example programs are provided to search arrays using sequential and binary search.

Uploaded by

Almas Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Sequential Search: 7.10 Write A Programe That Initialize An Array .It

Sequential search is a linear search that searches through an array element by element to find a target value. It checks each element against the target and stops when a match is found. Binary search is a faster search for sorted arrays that locates the middle element, compares it to the target, and recursively searches half of the array. It returns the index if a match is found or indicates the target was not found. Example programs are provided to search arrays using sequential and binary search.

Uploaded by

Almas Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

sequential search

Sequential search is also called linear


search array desired value. It follow the
following arch .It is simple way to
searchstep:
Visit the first element of the array
Compare its value with required value
If the value of the array match with
desired value search
to next element the search complete

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();
}

You might also like