c语言 算法题 面试题,面试的一个题目,没做出来。请教好的算法。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

程序同上,重发一遍加了注释,改变了输出结果的顺序

--------------------------------------------------------------------------------------------------

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值