该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
程序同上,重发一遍加了注释,改变了输出结果的顺序
--------------------------------------------------------------------------------------------------
#include
/*递归排序*/
void mysort(const int number, int *numlist, const int iInput);
/*交换两个数字指针的值*/
void myswap(int *First, int *Second);
main()
{
int i=0, iInput=0;
int numlist[10] = {0}; /*排序列表,内含需排序的各个数字*/
do
{
printf("Input a number \[
fflush(stdin); /*清空输入缓冲区*/
scanf("%d", &iInput);
}while(iInput<=0 || iInput>=10); /*如果输入数据不合法,则循环等待*/
for(i=1; i<=iInput; i++)
{
numlist[i-1] = i; /*赋值排序表*/
}
mysort(iInput, numlist, iInput);
printf("\n");
fflush(stdin);
getchar();
}
/*递归排序算法*/
/*算法思想是:排序的定义,第一个数字有n种选择,第二个数字有n-1种选择...*/
/* number: 需排序的数目 */
/* numlist: 需排序的数字列表 */
/* iInput: 输入的数字,也是最终输出结果的长度 */
void mysort(const int number, int *numlist, const int iInput)
{
int i=0;
/*如果剩余需排序的数目只有一个了,说明排序完成,输出结果*/
if(number == 1)
{
for(i=iInput-1; i>=0; i--)
{
printf("%d", *(numlist-i) );
}
printf("\t");
return;
}
/*递归算法*/
for(i=0; i
{
myswap(numlist, numlist+i);
mysort(number-1, numlist+1, iInput);
myswap(numlist, numlist+i);
}
}
/*交换算法*/
void myswap(int *First, int *Second)
{
int temp = 0;
temp = *First;
*First = *Second;
*Second = temp;
}