#include
using namespace std;
#define N 6 //宏定义需要进行排序的数字个数
int main()
{
int temp;
int list[N] = { 0 }; //定义数组用于存放无需的数列,数组里的元素初始化为0
cout << "请输入N个无序的数据:" << endl;
for (temp = 0; temp < N; temp++)
cin >> list[temp]; //依次从键盘上输入N个无序数据存放在数组list中
cout << "排序前的序列:" << endl; //先将排序前的数据输出,以便对比排序后的情况
for (temp = 0; temp < N; temp++)
cout << list[temp] << " ";
cout << endl;
void select_sort(int array[], int n); //函数声明
select_sort(list, N); //函数调用
cout << "排序后的序列:" << endl;
for (temp = 0; temp < N; temp++)
cout << list[temp] << " ";
cout << endl;
return 0;
}
void select_sort(int array[], int n)
{
int i, j, k, t;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
if (array[j] < array[k])
k = j;
t = array[k];
array[k] = array[i];
array[i] = t; //如果前面数据大于后面数据,则进行交换下标
}
}
选择法排序(C++实现)
最新推荐文章于 2023-09-04 11:33:22 发布