Ordinary sequential query
#include<iostream>
using namespace std;
int easy_sort(int arr[],int n,int key);
int main(){
int arr[8] = {1,2,3,4,5,6,7,8};
int index=easy_sort(arr,8,4);
cout<<" Index found :" <<index;
}
int easy_sort(int arr[],int n,int key){
for(int i=0;i<n;i++){
if(key==arr[i]) return i;
}
return -1;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
With sentries The order of the query
#include<iostream>
using namespace std;
int seq_search(int arr[],int n,int key);
int main(){
int arr[8] = {1,2,3,4,5,6,7,8};
int index=seq_search(arr,8,4);
cout<<" Index found :" <<index;
}
int seq_search(int arr[],int n,int key){
int i=n;
while(arr[i]!=key) i--;
return i;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.