C语言编程,用选择法对数组中4个整数按由大到小排序
1、代码如下:
#include<stdio.h>
#include<math.h>
#include<string.h>int main()
{void sort(int array[],int n);printf("测试开始\n");int nums[] = {2,3,4,1};sort(nums,4);int x;for(x=0;x<4;x++){printf("%d",nums[x]);}printf("测试结束\n");return 0;
}void 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[k]>array[j]){k = j;}}t = array[k];array[k]=array[i];array[i]=t;}
}
2、运行结果如下: