问题描述
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int ctr,inner,outer,didSwap,temp;
int nums[10];
time_t t;
srand(time(&t));
for (ctr = 0; ctr < 10; ctr++) {
nums[ctr] = (rand() % 99) + 1;
}
printf("\nHere is the list before the sort:\n");
for (ctr = 0; ctr < 10; ctr++) {
printf("%3d",nums[ctr]);
}
// Sorting the array
for (outer = 0; outer < 9; outer++) {
didSwap = 0;
for (inner = outer + 1; inner < 10; inner++) {
if (nums[inner] < nums[outer]) {
temp = nums[inner];
nums[inner] = nums[outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0) {
break;
}
}
printf("\n\nHere is the list after sorting:\n");
for (ctr = 0; ctr < 10; ctr++) {
printf("%3d",nums[ctr]);
}
printf("\n");
return 0;
}
它在大多数情况下都有效,但有时无法正确排序,有时甚至根本无法排序。
附言如果代码不正确,那么为什么它在 85% 的情况下都能正常工作。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)