ssearch3.cpp
来自「data struct algorithm and application in」· C++ 代码 · 共 22 行
CPP
22 行
// sequential search of sorted array
#include <iostream.h>
template<class T>
int SequentialSearch(T a[], const T& x, int n)
{// Search the unordered list a[0:n-1] for x.
// Return position if found; return -1 otherwise.
a[n] = x; // assume extra position available
int i;
for (i = 0; a[i] != x; i++);
if (i == n) return -1;
return i;
}
void main(void)
{
// a[7] is not used
int a[8] = {0, 5, 3, 4, 7, 2, 6, 0};
cout << SequentialSearch(a, 4, 7) << endl;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?