【问题标题】:Is Big O time complexity of this sorting code n^2?这个排序代码的大 O 时间复杂度是 n^2 吗?
【发布时间】:2015-06-14 07:10:22
【问题描述】:

我们知道冒泡排序数组的时间复杂度为 O(n^2)。

我想过使用冒泡排序对单链表进行排序。

下面是我的 C 代码。 (列表中的head 是通过引用headRef 传递的)。它确实排序正确,但我的问题是我发现时间复杂度的概念有点混乱,所以虽然我认为我下面的代码也有 O(n^2) 的复杂度,但我不确定。

void sortNodes(struct node **headRef)
{
    struct node *head;
    int i, j, k;
    struct node *node_Pointer;
    struct node *node_Pointer_Left, *node_Pointer_Left_Left, *hold;
    printf("\nSorting started !\n--------------------\n");

    for (i=1; i<N; i++)
    {
            head = *headRef;
            node_Pointer = head;
            for(j=1;j<N;j++)
            {
                    {
                            // Making node_Pointer point to the jth node.
                            // Making node_Pointer_Left point to (j-1)th node.
                            // Making node_Pointer_Left_Left point to (j-2)th node. 
                            if (j > 1)
                                    node_Pointer_Left_Left = node_Pointer_Left;
                            node_Pointer_Left = node_Pointer;
                            node_Pointer = node_Pointer->next;
                    }

                    if ( node_Pointer->data < node_Pointer_Left->data )
                    {
                            //Below  part sorts by changing pointers.
                            //If you want to sort by swapping data, please comment below part.
                            //and uncomment the bottom part.
                            struct node *old_J_next = node_Pointer->next;
                            node_Pointer->next = node_Pointer_Left;
                            node_Pointer_Left->next = old_J_next;
                            if (j ==1)
                                    *headRef  = node_Pointer;
                              else
                                    node_Pointer_Left_Left->next = node_Pointer;

                            hold = node_Pointer;
                            node_Pointer = node_Pointer_Left;
                            node_Pointer_Left = hold;

                            //Below commented part sorts by changing (swapping) the data.
                            /*{
                                    int temp = node_Pointer->data;
                                    node_Pointer->data = node_Pointer_Left->data;
                                    node_Pointer_Left->data = temp;
                            }*/
                    }
            }
    }
    printf("sorting finished !\n");

}

请确认上述代码的时间复杂度是否为O(N^2)。

我的分析:外部 for 循环执行 N-1 次,对于外部 for 循环的每次迭代,内部 for 循环执行 N-1 次。并且,对于内部 for 循环的每次迭代,都有一些“恒定”数量执行的语句。所以总执行(近似)= (N-1) * ((N-1)*constant) 这将是 N^2 的顺序。

很遗憾,我不确定我的分析是否正确,所以我将其发布在这里。如果你告诉它是对还是错,真的很感谢。

【问题讨论】:

  • 你的分析是正确的
  • ^感谢您的遵守。
  • 你总是可以用不同大小的随机列表来测试它,然后凭经验确定它。
  • 我同意 Lashane。顺便说一句,它也不是评论表明的冒泡排序。如果对剩余待排序段中的相邻元素的任何扫描没有产生交换,则冒泡排序有一个提前退出子句来停止。它是算法的属性,可在已排序的序列上提供 O(N) 的最佳情况性能。
  • 我只是想知道,为什么这个问题会被否决?为了写这个问题,我付出了相当大的努力。我也试图清楚地写出这个问题(这样阅读这个问题的人就会明白我想问什么)。我也认为这个问题很有用。该网站表示,如果问题缺乏研究努力,不清楚或有用,请投反对票。我认为这个问题很清楚也很有用。但是,我不确定“研究”部分,但我很确定在将问题发布到此处之前,我自己已经付出了一些“努力”来分析问题。我不需要赞成票,但为什么要反对票?

标签: c algorithm big-o bubble-sort


【解决方案1】:

您的分析是正确的。 O(n^2) 是。

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多