【问题标题】:Sorting a stack using another stack使用另一个堆栈对堆栈进行排序
【发布时间】:2015-01-14 10:50:24
【问题描述】:

我想知道该算法的时间复杂度,该算法用于使用另一个堆栈对堆栈进行排序。我以为是 O(N^2),但显然它看起来不止于此。

public static Stack<Integer> sort(Stack<Integer> s) {
    Stack<Integer> r = new Stack<Integer>();

    while(!s.isEmpty()) {
        int tmp = s.pop();
        while(!r.isEmpty() && r.peek() > tmp) {
            s.push(r.pop());
        }
        r.push(tmp);
    }
    return r;
}

【问题讨论】:

  • 尝试找出一个输入(s的内容),这将是该算法最坏的情况
  • 在我看来它应该是 O(n^2),是什么让你认为它不是?

标签: performance algorithm sorting complexity-theory time-complexity


【解决方案1】:

这个问题可以在 o(n^2) 复杂度中完成。这可以在不使用弹出最大数字并将其余元素存储在第二个堆栈中并更新第一个堆栈的大小,然后推回第一个堆栈的情况下完成stack.看看代码sn-p。

   #include<stdio.h>
            func(struct stack *s1)
            {
                            struct stack* s2=(struct stack*) malloc(sizeof(struct stack))
                            int i=0;
                            int max=INT_MIN;
                            size=s1->size;
                            if(s1->size==0)
                            {
                                return;
                            }
                            for(;i<size;i++)
                            {

                                while(size(s1)!=size)//popping the elements and pushing in s2 stack and keeping track of maximum element.
                                {

                                    temp=pop(s1);
                                    if(temp>max)
                                    {

                                        push(s2,max);
                                        max=temp;
                                    }

                                }
                                push(s1,max);//pushing the max element into stack s1 back and updating the size in push operation.
                                while(!empty(s2))//pushing extracted numbers back into stack s1 from s2.
                                {
                                    push(s1,pop(s2));

                                }

                            }


        }

【讨论】:

  • 虽然提供代码是回答问题的好方法,但最好就代码的作用、您为解决问题所做的工作等提供某种解释。
【解决方案2】:

如果排序堆栈[x_2, .., x_n](堆栈向右增长)花费t(n-1)时间,排序堆栈[x_1, .., x_n]时间将执行以下操作

  1. 对[x_2, .., x_n] 的s 的子堆栈进行排序
  2. 将x_1 弹出到tmp
  3. 最多将n-1元素从r转移到s
  4. 将x_1 推送到r
  5. 再次处理在步骤 3 中传输的元素,但它们的顺序使得内部 while 循环永远不会运行。

因此在[x_1, .., x_n] 上运行算法最多需要t(n-1) + O(n) 时间。这导致(对于一些常量c)

t(n) <= O(n) + t(n-1) <= c * n + t(n-1)
t(n) <= c * n + c * (n - 1) + t(n-2) <= ... <= c * (1 + 2 + ... + n)
t(n) <= c * n(n + 1) / 2 

所以t(n) 是O(n^2)。

【讨论】:

【解决方案3】:

在我看来 O(n^2)。我猜一个已经排序的堆栈的性能最差。在给定特定大小的已排序堆栈的情况下,我计算了 s.push 被执行的次数。

Stack of size 1. backpushes: 0
Stack of size 2. backpushes: 1
Stack of size 3. backpushes: 3
Stack of size 4. backpushes: 6
Stack of size 5. backpushes: 10
Stack of size 6. backpushes: 15
Stack of size 7. backpushes: 21
Stack of size 8. backpushes: 28
Stack of size 9. backpushes: 36

0,1,3,6,10 是triangular numbers 的序列。大小为 N 的排序堆栈需要 (N^2 + N)/2 次反推。这使得它 O(N^2)。

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2021-11-02
    • 2011-06-17
    • 2019-05-05
    • 2020-12-04
    • 2013-05-13
    • 2011-12-08
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多