//利用选择法对10个数进行排序
#include <iostream>
using namespace std;
int a[11];//因为后面都要用到,故要声明为全局变量
int main()
{
freopen ( "debug\\in.txt","r",stdin ); //输入数据将从in.txt文件中读取
//------声明函数
void getfun();//获取数据的函数
void sort_xuanze();//选择排序法
void plant();//输出函数
//------调用函数
getfun();
sort_xuanze();
plant();
fclose(stdin);//关闭文件
return 0;
}
void getfun()
{
cout<<"请输入这个数组:"<<endl;
for (int i=1; i<=10; i++)
{
cin >> a[i];
cout << "a[" << i << "]=" << a[i] <<" ";
}
cout<<endl;
}
//选择排序法
void sort_xuanze()
{
int min;
for (int i=1; i<=10; i++)
{
min=i;
for (int j=i+1; j<=10; j++)
{
if ( a[j]<a[min] )
{
min=j;
}
}
swap( a[i],a[min] );
}
}
//显示排序后的数组
void plant()
{
cout<<"排序后的数组为:"<<endl;
for (int i=1; i<=10; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
对数组进行选择排序
最新推荐文章于 2022-10-03 14:13:08 发布