从当前未排序的整数中找一个最小的整数,将它放在已排序的整数列表的最后。

要点:选择排序选最小的,往左边选。

#include <iostream>

using namespace std;

void SelectSort(int List[],int size);

int main()
{
    int a[] = {2,3,1,9,8,7,6,5,4,0};
    SelectSort(a, 10);
    for(int i = 0; i < 10; i++)
        cout << a[i] << endl;

    system("pause");
    return 0;
}

void SelectSort(int List[],int size)
{
    for(int i = 0; i < size -1 ; i++)
    {
        int min = i;
        for(int j = i + 1; j < size; j++)
        {
            if(List[min] > List[j])
                min = j;
        }
        std::swap(List[i], List[min]);
    }
}

 

posted on 2015-03-10 10:47  箭已离弓  阅读(93)  评论(0编辑  收藏  举报