直接选择排序

原博客: 白话经典算法系列之八 MoreWindows白话经典算法之七大排序总结篇
#include <iostream>
#include <vector>
using namespace std;

void directselectsort(vector<int> &vi){
    if(vi.size()<=1) return ;
    int n=vi.size();
    for(int i=0;i<n-1;++i){//当选择完n-1个时,数组已经有序了
        int temp=i;
        for(int k=i+1;k<n;++k){
            if(vi[k]<vi[temp])
                temp=k;
        }
        swap(vi[i],vi[temp]);
    }
}

int main(){
    vector<int> vi;
    vi={1,8,2,3,7,6,9,5,4};
    directselectsort(vi);
    for(auto ieh: vi)
        cout<<ieh<<" ";
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/o1101574955/article/details/75458172
今日推荐