Sequential Search:
Sequential search is also called linear search array desired value. It
follow the following search steps. It is simple way to search step.
Visit the first element of the value.
Compare its value with required value.
If the value of the array match with desired value search
to next element the search complete.
Program:
Write a program that initialize an array. It input a value from user and
search the number in the array.
#include <iostream.h>
#include<conio.h>
Void main()
int arr[10]={10,20,30,40,50,60,70,80,90,100};
int i ,n ,loc=-1;
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:
Binary search is quicker method of searching for values in the array.
Binary search is quicker method but it can only search sorted array. It
cannot apply on unsorted array. It locate the middle element of the array
compare with search element. 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 element. If the search number is less then middle
element, it search the first half of the array.
Program: Write a
program that initialize an array of ten integers .It inputs integers from the
user and search the value in the array using the binary search.
#include <iostream.h>
#include <conio.h>
void main()
clrscr();
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 find ";
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();