美文网首页
选择排序O(C++,n^2)

选择排序O(C++,n^2)

作者: 陈_振 | 来源:发表于2019-05-17 20:26 被阅读0次
#include <iostream>
//#include <algorithm>
using namespace std;

void selectionSort(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        int minIndex = i;
        // 寻找[i,n)区间里的最小值
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        swap(arr[i], arr[minIndex]);
    }
}

int main(int argc, const char * argv[]) {
    
    int arr[] = {5,4,3,2,1,0};
    selectionSort(arr, 6);
    
    for (int i = 0; i < 6; i++) {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    
    return 0;
}

相关文章

网友评论

      本文标题:选择排序O(C++,n^2)

      本文链接:https://www.haomeiwen.com/subject/watwaqtx.html