您的位置:首页 > 其它

直接选择排序举例

2010-05-31 20:52 281 查看
#include<iostream>
template<class T>
void Swap(T &x,T &y)
{
T temp;
temp=x;
x=y;
y=temp;
}

template <class T>
void SelectionSort(T A[],int n)
{
int smallIndex;
int i,j;

for(i=0;i<n-1;i++)
{
smallIndex=i;
for(j=i+1;j<n;j++)
if(A[j]<A[smallIndex])
smallIndex=j;
Swap(A[i],A[smallIndex]);
for(int k=0;k<n;k++)
cout<<A[k]<<" ";
cout<<endl;
}
}

int main()
{
int data1[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
cout << "排序前的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
cout << "开始排序..." << endl;
SelectionSort(data1, 20);
cout << "排序后的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: