选择排序算法(C++版)

在这里插入图片描述

#include <iostream>

using namespace std;

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

int main()
{
    int a[10] = {10,9,8,7,6,5,4,3,2,1};
    selectionSort(a,10);
    for(int i = 0;i < 10;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}
发布了251 篇原创文章 · 获赞 28 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xiong_xin/article/details/104254505