#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
using namespace std;
#define MAXSIZE 10
const int n = MAXSIZE;
int num[n];
void init()
{
for (int i = 0; i < n; i++) num[i] = rand() % 100;//产生MAXSIZE个数据 数据范围为0~~~99;
}
void output()
{
for (int i = 0; i < n; i++)
{
printf("%d ", num[i]);
if (i % 10 == 9) printf("\n");
}
printf("\n");
}
///选择排序 order by asc
void myswap(int a, int b)
{
int t; t = num[a]; num[a] = num[b]; num[b] = t;
}
void Selectsort()
{
int i, j,k;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (num[j] < num[k]) k = j;
}
if (k != i) myswap(k, i);
}
}
int main()
{
srand((unsigned)time(NULL));
init();
output();
//
printf("选择排序:\n");
Selectsort();
;
/
output();
return 0;
}
29.选择排序
最新推荐文章于 2019-08-21 09:14:47 发布