线性搜索的最坏情况时间复杂度为 o(n)。
input: arr[] = { 12, 35, 69, 74, 165, 54}sea=165output: 165 is present at location 5.
说明线性搜索(搜索算法),用于查找给定的数字是否存在于数组中以及如果存在则出现在什么位置。它也称为顺序搜索。它很简单,工作原理如下:我们继续将每个元素与要搜索的元素进行比较,直到找到它或列表结束。
示例#include <iostream>using namespace std;int main() { int sea, c, n=6; int arr[] = { 12, 35, 69, 74, 165, 54}; sea=165; for (c = 0; c < n; c++) { if (arr[c] == sea) { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d isn't present in the array.\n", search); return 0;}
以上就是线性搜索的c/c++程序?的详细内容。