Performance 使用另一个堆栈对堆栈进行排序

Performance 使用另一个堆栈对堆栈进行排序,performance,algorithm,sorting,complexity-theory,time-complexity,Performance,Algorithm,Sorting,Complexity Theory,Time Complexity,我想知道该算法的时间复杂度,该算法用于使用另一个堆栈对堆栈进行排序。我以为它是O(N^2),但显然它看起来不止如此 公共静态堆栈排序(堆栈){ 堆栈r=新堆栈(); 而(!s.isEmpty()){ int tmp=s.pop(); 而(!r.isEmpty()&&r.peek()>tmp){ s、 推(r.pop()); } r、 推送(tmp); } 返回r; } 在我看来是O(n^2)。我猜已经排序的堆栈具有最坏的性能。我计算了s.push在给定特定大小的已排序堆栈的情况下执行的次数 St

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

公共静态堆栈排序(堆栈){
堆栈r=新堆栈();
而(!s.isEmpty()){
int tmp=s.pop();
而(!r.isEmpty()&&r.peek()>tmp){
s、 推(r.pop());
}
r、 推送(tmp);
}
返回r;
}
在我看来是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是的序列。大小为N的已排序堆栈需要(N^2+N)/2次反向推送。这就是O(N^2)。

如果排序堆栈
[x_2,…,x_N]
(堆栈向右增长)需要
t(N-1)
时间,则排序堆栈
[x_1,…,x_N]
时间将执行以下操作

  • 排序子堆栈
    [x_2,…,x_n]
    s
  • Pop
    x_1
    tmp
  • 最多将
    n-1
    元素从
    r
    传输到
    s
  • x\u 1
    r
  • 再次处理步骤3中传输的元素,但它们的顺序是内部while循环while从不运行
  • 因此,在
    [x_1,…,x_n]
    上运行算法最多需要
    t(n-1)+O(n)
    时间。这导致(对于某些常量
    c

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

    #包括
    func(结构堆栈*s1)
    {
    结构堆栈*s2=(结构堆栈*)malloc(sizeof(结构堆栈))
    int i=0;
    int max=int_MIN;
    大小=s1->大小;
    如果(s1->size==0)
    {
    返回;
    }
    对于(;imax)
    {
    推力(s2,最大值);
    最大值=温度;
    }
    }
    推力(s1,最大值)//将max元素推回堆栈s1,并在push操作中更新大小。
    而(!empty(s2))//将提取的数字从s2推回到堆栈s1中。
    {
    推(s1,弹出(s2));
    }
    }
    }
    
    试着找出一个输入(s的内容),这将是该算法最坏的情况。我觉得它应该是O(n^2),什么让你认为它不是?此外,OP的算法本质上是一个实现,而提供代码是回答问题的好方法,最好提供一些解释,说明代码在做什么,您做了什么来解决问题,等等。
    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 
    
       #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));
    
                                    }
    
                                }
    
    
            }