#include
#define N 8
void selection_sort(int a[], int n) {
int i, t, imax = 0;
if(n < 1) return;
for(i = 1; i < n; ++i) {
if(a[imax] < a[i])
imax = i;
}
if(imax != n - 1) {
t = a[n - 1];
a[n - 1] = a[imax];
a[imax] = t;
}
selection_sort(a, n - 1);
}
int main(void) {
int i, a[N] = {8,5,4,6,1,2,3,7};
printf("排序前:\n");
for(i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
selection_sort(a, N);
printf("排序后:\n");
for(i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
#include//递归应该用函数递归。。。不然你会想不清楚的
#define N 8
int selection_sort(int a[], int n){
int max,k,change;
if(n==1)
return 0;
max = a[0];
for(int i = 0; i < n; i++){
if(max <= a[i]){
max = a[i];
k=i;
}
}
change=a[k];
a[k]=a[n-1];
a[n-1]=change;
return selection_sort(a,n-1);
}
int main(void){
int a[N], n;
n = N;
printf("录入数组a的各个元素:");
for(n = 0; n < N; n++)
scanf("%d", &a[n]);
a[n] = selection_sort(a, N);
printf("按照从大到小的顺序输出数组a为:");
for(n = 0; n < N; n++)
printf("%d ", a[n]);
printf("\n");
return 0;
}