直接选择法排序

来源:互联网 发布:简述云计算 编辑:程序博客网 时间:2024/06/11 16:23
#include <iostream>using namespace std;#include <ctime>typedef int T;void sort(T a[], int n)  //T * p, int n{for (int i=0; i<n-1; i++){int pos = i;for (int j=i+1; j<n; j++)if (a[j] < a[pos])pos = j;swap(a[pos], a[i]);}}int main(){const int N = 10240;int a[N];for (int i=0; i<N; i++)a[i] = N-i;time_t t1=time(NULL);sort(a, N);time_t t2 = time(NULL);cout << "time:" << t2-t1 << endl;for (int i=0; i<10; i++)cout << a[i] << ' ';cout << endl;return 0;}