选择排序------直接选择排序

#include <iostream>
using namespace std;

template <class T>
void selectionSort(T A[], int n)
{
    int smallIndex=0;

    for(int i=0;i<n-1;i++)
    {
        smallIndex=i;
        for(int j=i+1;j<n;j++)
        {
            if(A[j]<A[smallIndex])
                smallIndex=j;
        }
        swap(A[i], A[smallIndex]);
    }
}

 

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

 

直接选择排序,  从前到后, 依次选择出待排序数据的最小值并将其与待排序数据的首数据相交换。

 

posted on 2017-01-25 15:11  Angry_Panda  阅读(152)  评论(0编辑  收藏  举报

导航