简体   繁体   English

该算法的时间复杂度如何?为什么?

[英]what would be the time complexity of this algorithm and why?

could anyone please explain me the time complexity of this code. 谁能解释一下这段代码的时间复杂度。 Thanks 谢谢

public static Stack<Integer> sortStack(Stack<Integer> aStack) {

    Stack<Integer> rStack=new Stack<>();
    int temp=0;

    rStack.push(aStack.pop());

    while(!aStack.empty()){
        temp=aStack.pop();

        while(!rStack.empty() && temp >rStack.peek()){
            aStack.push(rStack.pop());
        }
        rStack.push(temp);
    }
    return rStack;
}

我认为这将是O(n ^ 2),因为内部while的时间复杂度是n, while外部while的时间复杂度是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM