# Realization of sequential search algorithm with sentinel in C + +

Time：2021-4-15

The sentinel is often a variable in the program. If it is to sort the numbers, then the variable is generally a numerical variable. Variable assignment is equivalent to sentry. When there is a value equal to sentry in the sorting sequence or there is a certain relationship, you can do an operation, such as stop sorting or carry out the next sorting.

give an example:

The algorithm of sequential retrieval is described as follows

``````int Search_Sequen(SSTable ST,KeyType key){
//In the linear table st, the data elements whose key is equal to key are searched sequentially,
//If found, the function value is the position of the element in the table, otherwise - 1
ST.element [ ST.length ]. key = key; // set the sentry
i=0;
while(ST.element[i].key!=key) i++;
if(i<ST.length)
return i;
else
return -1;
}``````

text

Before doing exercises on niuke.com, I found this unique order query. When I first heard the saying of “sentinel”, I checked it

The specific implementation is to empty the 0 position of the array, and insert the key to be searched as the monitor when searching

The advantage of this method is that there is no need to check whether the search ends every time, which reduces the number of element comparisons,

The final return value is either the element subscript or the 0 th bit of the array (in this case, the watchpost)

Here is my code

``````
#include <iostream>
using namespace std;

template<class T>
int linear_search(T& arr,int key)
{
int length = sizeof(arr) / sizeof(arr[0]);
int i = length;
arr[0] = key;
while (arr[i] != key)
{
i--;
}
return i;
}

int main()
{
int array[] = { 0, 7,9,10,11,15 };
int len = sizeof(array) / sizeof(array[0]);
cout << linear_search(array, 10);
return 0;
}``````

By the way, there will be one

Error c2760: syntax error: unexpected token ‘identifier’, expected token ‘;’

I don’t quite understand the specific principle of this mistake. I just give a solution:

Project > Properties > C / C + + > language > conform to pattern > no

Finally, I want to remind myself that as a function parameter, an array is the pointer to the first place of the array, and the pointer does not have other attributes of the array,

So to get the length of the array in the function, you can only pass in the array itself in the form of reference and template, so that you can get the length of the array with sizeof()

summary

So far, this article about C + + implementation of sequential search with sentinel is introduced here. For more related C + + sentinel sequential search content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!

## Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]