程序代码:
#include"stdio.h"
#include"stdlib.h"
#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
int c;
c = a;
a = b;
b = c;
}
void select_sort(int a[], int n)
{
printf("the unsort data is:");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
cout << endl;
int min;
int i, j;
int xiabiao;
for (i = 0; i < n-1; i++)
{
min = 999;
for (j = i; j < n; j++)
{
if (a[j] < min)
{
min = a[j];
xiabiao = j;
}
}
swap(a[i], a[xiabiao]);
printf("the %dth sort data is:",i+1);
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
cout << endl;
}
printf("the sort data is:");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
cout << endl;
}
int main()
{
int a[10] = { 2, 5, 1, 3, 8, 7, 6, 4, 10, 9 };
select_sort(a, 10);
system("pause");
return 0;
}
实验结果: