Redian新闻
>
Sort a stack without using auxiliary stacks or arrays
avatar
Sort a stack without using auxiliary stacks or arrays# Programming - 葵花宝典
b*a
1
Design an efficient algorithm to sort elements in a stack in either
ascending/descending order, using only pop(), top(), push(), isEmpty(),
isFull(). Do not use any auxiliary stacks or arrays.
I found this question in jobhunting board. the anwser given by a niu ren is
Is this right? Does not look like right one.Thanks.
avatar
c*t
2
You didn't post the complete code. What you looked at was mostly a
for-loop.

is

【在 b**a 的大作中提到】
: Design an efficient algorithm to sort elements in a stack in either
: ascending/descending order, using only pop(), top(), push(), isEmpty(),
: isFull(). Do not use any auxiliary stacks or arrays.
: I found this question in jobhunting board. the anwser given by a niu ren is
: Is this right? Does not look like right one.Thanks.

avatar
E*V
3
seems right
why dont' you test it?

is

【在 b**a 的大作中提到】
: Design an efficient algorithm to sort elements in a stack in either
: ascending/descending order, using only pop(), top(), push(), isEmpty(),
: isFull(). Do not use any auxiliary stacks or arrays.
: I found this question in jobhunting board. the anwser given by a niu ren is
: Is this right? Does not look like right one.Thanks.

avatar
w*g
4
This is a recursive algorithm, and it actually uses an auxiliary stack
, though not obvious to newbies. This is actually more dangerous than
explicitly using some auxiliary data structure because recursion may lead to
stack overflow.

is

【在 b**a 的大作中提到】
: Design an efficient algorithm to sort elements in a stack in either
: ascending/descending order, using only pop(), top(), push(), isEmpty(),
: isFull(). Do not use any auxiliary stacks or arrays.
: I found this question in jobhunting board. the anwser given by a niu ren is
: Is this right? Does not look like right one.Thanks.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。