如何“内在”和“外部”在这个气泡排序代码中工作?

发布于 2025-02-08 12:56:54 字数 1173 浏览 0 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2025-02-15 12:56:54

内部永远不会变成nums [0]Inner外部是数组索引,因此,当Inner是 0 时,,nums [innions] 是num [0]

执行比较和交换的代码从不使用Innerofter,它使用nums [innions]nums [out ]

        if (nums[inner] < nums[outer])
        {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
            didSwap = 1;
        }

inner never becomes nums[0]. inner and outer are array indexes, so when inner is 0, nums[inner] is nums[0].

The code that performs the comparison and swapping never does that with inner and outer, it uses nums[inner] and nums[outer].

        if (nums[inner] < nums[outer])
        {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
            didSwap = 1;
        }
娇纵 2025-02-15 12:56:54

我试图学习c。

的书中的泡沫排序算法

对于首先,如果确实来自一本书,那么我建议停止阅读本书,因为这本书的作者似乎是一位低资格的程序员。

提出的方法不是气泡排序方法。

它看起来像是选择排序的方法,并具有冗余掉期。
此外,代码包含一个严重的错误。

例如,以类似的数组为单位

int nums[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

,并且不会对数组进行排序!

内部循环在做什么?

它在当前元素nums [outer]之后遍历数组>将元素交换并将标志didswap设置为1(标志意味着某些元素已交换,外循环应继续其迭代),

        if (nums[inner] < nums[outer])
        {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
            didSwap = 1;
        }
    }

否则,如果没有交换

    if (didSwap == 0)
    {
        break;
    }

但是,即使对于当前nums [outer],选择排序方法也必须继续其迭代] 。

这是一个演示程序,该程序表明提出的排序方法(不是气泡排序方法)不起作用。

#include <stdio.h>

int main( void )
{
    int ctr, inner, outer, didSwap, temp;
    int nums[3] = { 1, 3, 2 };

    for (outer = 0; outer < 2; outer++)
    {
        didSwap = 0;
        for (inner = outer; inner < 3; 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 < 3; ctr++)
    {
        printf("%i\n", nums[ctr]);
    }
}

程序输出是

 Here is the list after the sort: 
1
3
2

您可以看到的,该数组未被排序。:)

I am trying to learn the bubble sort algorithm in a book for C.

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

int nums[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

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 element nums[inner] that is less than nums[outer] it swaps the elements and sets the flag didSwap to 1 (the flag means that some elements were swapped and the outer loop shall continue its iterations)

        if (nums[inner] < nums[outer])
        {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
            didSwap = 1;
        }
    }

Otherwise if there are no swaps then the control exits the outer loop

    if (didSwap == 0)
    {
        break;
    }

However the selection sort method must continue its iteration even if for the current nums[outer] there was not found an element nums[inner] less than nums[outer].

Here is a demonstration program that shows that the presented sort method (that is not the bubble sort method) does not work.

#include <stdio.h>

int main( void )
{
    int ctr, inner, outer, didSwap, temp;
    int nums[3] = { 1, 3, 2 };

    for (outer = 0; outer < 2; outer++)
    {
        didSwap = 0;
        for (inner = outer; inner < 3; 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 < 3; ctr++)
    {
        printf("%i\n", nums[ctr]);
    }
}

The program output is

 Here is the list after the sort: 
1
3
2

As you can see the array was not sorted.:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文