C ++程序使用线性搜索在数组中查找最小元素
这是一个使用线性搜索方法查找数组的最小元素的C++程序。该程序的时间复杂度为O(n)。
算法
Begin Assign the data element to an array. Assign the value at ‘0’ index to minimum variable. Compare minimum with other data element sequentially. Swap values if minimum value is more then the value at that particular index of the array. print the minimum value. End
范例程式码
#include<iostream>
using namespace std;
int main() {
int n, i, minimum, a[10] = {1, 6, 7, 10, 12, 14, 12, 16, 20, 26};
char ch;
minimum = a[0];
cout<<"\nThe data element of array:";
for(i = 0; i < 10; i++) {
cout<<" "<<a[i];
if(minimum > a[i])
minimum= a[i];
}
cout<<"\n\nMinimum of the data elements of array using linear search is: "<<minimum;
return 0;
}输出结果
The data element of array: 1 6 7 10 12 14 12 16 20 26 Minimum of the data elements of array using linear search is: 1
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短