SelectSort

//SelectSort
//选择最小的,交换到左边 
#include<iostream>
using namespace std;
void SelectSort (int a[],int n)
{
    for(int i=0;i<n;i++)
    {
        int min =i;//记录最小的值
        for(int j=i+1;j<n;j++) 
        {
            if(a[j]<a[min])
            {
                min=j;
            }
        }
        swap(a[i],a[min]);
    }
}
int main()
{
    int x[]={1,3,5,7,9,0,2,4,6,8};
    SelectSort(x,10);
    for(int i=0;i<10;i++)
    {
        cout<<x[i]<<" ";
        cout<<endl;
     } 
    return 0;
}

 

posted @ 2019-08-05 23:51  Maggieisxin  阅读(121)  评论(0编辑  收藏  举报