direct selection sort

Original blog: The eighth of the classic vernacular algorithm series MoreWindows vernacular classic algorithm seven sorting summary articles
#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){//When n-1 items are selected, the array is already in order
        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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859433&siteId=291194637