如何“内在”和“外部”在这个气泡排序代码中工作?
我正在尝试在C的书中学习泡沫排序算法。 nums
数组就像Inner
变成NUM [0]
时 ofter 变成nums [1]
(如果我正确, ),然后通过循环进行?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int 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;
}
puts("\n Here is the list before the sort: ");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%i\n", nums[ctr]);
}
for (outer = 0; outer < 9; outer++)
{
didSwap = 0;
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums[outer])
{
temp = nums[inner];
nums[inner] = nums[outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
puts("\n Here is the list after the sort: ");
for ( ctr = 0; ctr < 10; ctr++)
{
printf("%i\n", nums[ctr]);
}
return 0;
}
I am trying to learn the bubble sort algorithm in a book for C. I can't seem to understand in the code below how int outer
and int inner
link to which element of the nums
array. Like how does inner
become nums[0]
while outer
becomes nums[1]
(if I'm correct), then progresses through the loop?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int 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;
}
puts("\n Here is the list before the sort: ");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%i\n", nums[ctr]);
}
for (outer = 0; outer < 9; outer++)
{
didSwap = 0;
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums[outer])
{
temp = nums[inner];
nums[inner] = nums[outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
puts("\n Here is the list after the sort: ");
for ( ctr = 0; ctr < 10; ctr++)
{
printf("%i\n", nums[ctr]);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内部
永远不会变成nums [0]
。Inner
和外部
是数组索引,因此,当Inner
是 0 时,,nums [innions] 是
num [0]
。执行比较和交换的代码从不使用
Inner
和ofter
,它使用nums [innions]
和nums [out ]
。inner
never becomesnums[0]
.inner
andouter
are array indexes, so wheninner
is0
,nums[inner]
isnums[0]
.The code that performs the comparison and swapping never does that with
inner
andouter
, it usesnums[inner]
andnums[outer]
.对于首先,如果确实来自一本书,那么我建议停止阅读本书,因为这本书的作者似乎是一位低资格的程序员。
提出的方法不是气泡排序方法。
它看起来像是选择排序的方法,并具有冗余掉期。
此外,代码包含一个严重的错误。
例如,以类似的数组为单位
,并且不会对数组进行排序!
内部循环在做什么?
它在当前元素
nums [outer]
之后遍历数组>将元素交换并将标志didswap
设置为1(标志意味着某些元素已交换,外循环应继续其迭代),否则,如果没有交换
但是,即使对于当前
nums [outer]
,选择排序方法也必须继续其迭代] 。这是一个演示程序,该程序表明提出的排序方法(不是气泡排序方法)不起作用。
程序输出是
您可以看到的,该数组未被排序。:)
For starters if the presented code is indeed from a book then I advice to stop reading the book because it seems the author of the book is a low-qualified programmer.
The presented method is not the bubble sort method.
It looks like the selection sort method with redundant swaps.
And moreover the code contains a serious bug.
For example take an array like
and the array will not be sorted!
What is the inner loop doing?
It travers the array after the current element
nums[outer]
and if it finds an elementnums[inner]
that is less thannums[outer]
it swaps the elements and sets the flagdidSwap
to 1 (the flag means that some elements were swapped and the outer loop shall continue its iterations)Otherwise if there are no swaps then the control exits the outer loop
However the selection sort method must continue its iteration even if for the current
nums[outer]
there was not found an elementnums[inner]
less thannums[outer]
.Here is a demonstration program that shows that the presented sort method (that is not the bubble sort method) does not work.
The program output is
As you can see the array was not sorted.:)