# include <stdio.h>
//把一个数组按从大到小的顺序排序
//按照选择排序的方法实现
void arrChoose(int *arr,int n){
int arrMax,temp,k;
for (int i=0; i<n-1; i++) {
arrMax=arr[i];
for (int j=i+1;j<n;j++){
if (arrMax<arr[j]){
arrMax=arr[j];
k=j;
}
}
temp=arr[k];
arr[k]=arr[i];
arr[i]=temp;
}
printf("数组重新排序后\n");
for (int i=0; i<n; i++) {
printf("a[%d]=%d\n",i,arr[i]);
}
}
int main (){
int arr[]={1,14,35,3,46,5,6,89,33,67,36};
int m=sizeof(arr)/sizeof(int);
printf("数组排序前\n");
for (int i=0; i<m; i++) {
printf("a[%d]=%d\n",i,arr[i]);
}
arrChoose(arr,m);
return 0;
}
|
|