Sequential Search


1. Algorithm:

Sequential Search: the simplest algorithm to search a dictionary for a given key is to test successively against each element.

This works correctly regardless of the order of the elements in the list. However, in the worst case all elements might have to be tested.

Sequential_Search_intro

2. Source code:

#include<stdio.h>
#include<iostream>
using namespace std;
int a[100],n,x;
int Sequential_Search(int M[], int N, int X){
int k = 0;
while (M[k] != X && k < N)
k++;
if (k < N)
return k;
return 0;
}
void InputArray(){
cout<<"Input number of array: "; cin>>n;
for(int i=0;i<n;i++){
cout<<"a["<<i<<"]= "; cin>>a[i];
}
cout<<"Input number which want to search: "; cin>>x;
}
int main(){
InputArray();
cout<<"Position in Array: "<<Sequential_Search(a,n,x);
return 0;
}

3. Result:

Sequential_Search_Result


Advertisements