【发布时间】:2021-04-07 15:10:27
【问题描述】:
#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;
}
有时它不能正确地对列表进行排序,有时甚至根本不能排序。 Screenshot of error
【问题讨论】:
-
请编辑您的代码并正确编写,stackoverflow.com/questions/366588/…
-
这也不是冒泡排序。此算法中没有故意相邻的上下移动。
标签: c random bubble-sort