选择排序

来源:互联网 发布:庭院流水桌面软件 编辑:程序博客网 时间:2024/06/06 21:41
#include <iostream>#include <cstdlib>using namespace std;/* * name : selection sort * author : sangoly * O(n^2) * S(1) * date : 2014/4/15 */void selection_sort(int a[], int length){     for (int i=0; i<length-1; i++) {         int smallest = i;         for (int j=i+1; j<length; j++)             if (a[j] < a[smallest]) {                 int tmp = a[smallest];                 a[smallest] = a[j];                 a[j] = tmp;             }     }}int main(int argc, const char **argv){    int a[] = {3, 41, 52, 26, 38, 57, 9, 49};    selection_sort(a, 8);    for (int i=0; i<8; i++)        cout<<a[i]<<" ";    cout<<endl;    system("pause");    return 0;}

0 0
原创粉丝点击