C++选择排序

/*
    选择排序实例
    Wirtten by: nick
    Date: 2012-10-16 12:13
*/

#include <iostream>
#include <iomanip>

using namespace std;


void selectionSort(int list[], int last);
void exchangeSmallest(int list[], int current, int last);

int main()
{
    int a[10] = {3,4,6,3,2,8,4,0,5,7};

    selectionSort(a, 9);

    for(int i=0; i<10; i++)
        cout<< setw(5) << a[i];

    return 0;
}

void selectionSort(int list[], int last)
{
    for(int current=0; current<last; current++)
    {
        exchangeSmallest(list, current, last);
    }
}

void exchangeSmallest(int list[], int current, int last)
{
    int smallest = current;
    for(int walker=current+1; walker<=last; walker++)
    {
        if(list[smallest] > list[walker])
        {
            smallest = walker;
        }
    }
    int tmp = list[current];
    list[current] = list[smallest];
    list[smallest] = tmp;
}
posted @ 2012-10-16 12:15  wouldguan  阅读(249)  评论(0编辑  收藏  举报