练习69

#include <iostream>
using namespace std;


// 辅助函数:交换x和y的值
template <class T>
void swapData(T &x, T &y)
{
T temp;

temp = x;
x = y;
y = temp;
}


// 用选择法对数组A的n个元素进行排序
template <class T>
void selectSort(T a[], int n)
{
int smallIndex;    //每以趟中选出的最小元素之下标
int i, j;

// sort a[0]..a[n-2], and a[n-1] is in place
for (i = 0; i < n-1; i++) 
{
smallIndex = i;    //最小元素之下标初值设为i
// 在元素a[i+1]..a[n-1]中逐个比较显出最小值
for (j = i+1; j < n; j++) 
// smallIndex始终记录当前找到的最小值的下标
if (a[j] < a[smallIndex])
smallIndex = j;
// 将这一趟找到的最小元素与a[i]交换
swapData(a[i], a[smallIndex]);
//输出数据
for(int k=0;k<n;k++)
cout << a[k] << "  ";
cout << endl;
//结束输出

}
}


int main()
{
int i;

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(i=0;i<20;i++)
cout << data1[i] << "  ";
cout << endl;
cout << "开始排序..." << endl;
selectSort(data1, 20);
cout << "排序后的数据:" << endl;
for(i=0;i<20;i++)
cout << data1[i] << "  ";
cout << endl;
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值