/*
Copyright (c) 烟台大学计算机与控制工程学院
Author:刘慧艳
Created:
Edition:V1.0
Describe: 数组的排序
将项目4中的排序算法换作为选择排序,完成相同的任务
*/
#include <iostream>
using namespace std;
void select_sort(int arr[],int num);
void output_array(int arr[],int num);
int main()
{
int a[20]= {86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};
int b[15]= {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};
select_sort(a,20);
output_array(a,20);
select_sort(b,15);
output_array(b,15);
return 0;
}
void select_sort(int arr[],int num)
{
int i,j,k,t;
for(i=0; i<num-1; ++i)
{
k=i;
for(j=i+1; j<num; ++j)
if(arr[j]<arr[k])
k=j;
t=arr[k];
arr[k]=arr[i];
arr[i]=t;
}
return;
}
void output_array(int arr[],int num)
{
int i;
for(i=0; i<num; ++i)
cout<<arr[i]<<" ";
cout<<endl;
}
选择法排序
最新推荐文章于 2023-11-06 18:57:56 发布