练习 03-04-02
//用选择法对数组中10个整数按照由小到大的顺序排序
输出样例:
array A =
8 1 4 3 7 9 1 4 7 8
The sorted array A =
1 1 3 4 4 7 7 8 8 9
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((unsigned)time(NULL));
void sort(int array[],int n);
int a[10],i;
printf("array A =\n");
for(i=0;i<10;++i){
a[i]=rand()%10;
printf("%5d",a[i]);
}
sort(a,10);
printf("\nThe sorted array A =\n");
for(i=0;i<10;++i){
printf("%5d",a[i]);
}
return 0;
}
void sort(int array[],int n)
{
int i,j,temp;
for(i=0;i<n;++i){
for(j=i+1;j<n;++j){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}