Find the first minimum number and the second one.

来源:互联网 发布:王珊数据库视频教程 编辑:程序博客网 时间:2024/05/18 04:15

在一个数组中找出最小的两个数,复杂度为O(n);

#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <time.h>int a[10000000];int main() {    srand(time(NULL));    int i;    for (i = 0; i < 10000; i++)        a[i] = rand();    long start = clock();    int min1 = a[0], min2 = a[0];    for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) {        if (a[i] < min1){            min2 = min1;            min1 = a[i];        }    }    printf ("%d\n%d", min1, min2);    printf ("\nTime is %d ms", clock() - start);    putchar ('\n');    system ("pause");    return 0;}