数组排序 _ 递归 _ 求助帖
#include<stdio.h>#define N 10
void in_array(int a[],int n)
{
int i;
printf("Please input ten of integer :\n");
for(i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a);
}
}
void out_array(int a[],int n)
{
int i;
printf("we will output the array :\n");
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
}
void sort(int a[],int n)
{
int f; //f是旗帜
int j; //j是下标,从0开始后移
if(n>1)
{
sort(a,n-1);
f = a[n-1];
j = n-2;
while(a[j]>f&&j>=0)
{
a[j] = a[j+1];
j--;
}
a[j+1] = f;
}
}
void main()
{
int a[N];
in_array(a,N);
out_array(a,N);
printf("现在进行排序哦,看好了!\n");
sort(a,N);
out_array(a,N);
}
输出结果:
Please input ten of integer :
a[0] = 10
a[1] = 9
a[2] = 8
a[3] = 7
a[4] = 6
a[5] = 5
a[6] = 4
a[7] = 3
a[8] = 2
a[9] = 1
we will output the array :
a[0] = -858993460
a[1] = -858993460
a[2] = -858993460
a[3] = -858993460
a[4] = -858993460
a[5] = -858993460
a[6] = -858993460
a[7] = -858993460
a[8] = -858993460
a[9] = -858993460
现在进行排序哦,看好了!
we will output the array :
a[0] = -858993460
a[1] = -858993460
a[2] = -858993460
a[3] = -858993460
a[4] = -858993460
a[5] = -858993460
a[6] = -858993460
a[7] = -858993460
a[8] = -858993460
a[9] = -858993460
Press any key to continue