reverse(stack *s){
IsEmpty(s)
return;
top = pop(s);
reverse(s);
ascending(s, top);
}
ascending(stack *s, int top){
IsEmpty(s){
push(top);
return;
}
i = pop(s);
if(i top){
ascending(s, top);
push(i);
}
else{
ascending(s, i);
push(top);
else{
ascending(s, i);
push(top);
}
}
@swinivas:why have you used ascending(s,i) here?
On Sat, Sep 11, 2010 at 6:40 PM, Srinivas lavudyasrinivas0...@gmail.comwrote:
reverse(stack *s){
IsEmpty(s)
return;
top = pop(s);
reverse(s);
ascending(s, top);
}
ascending(stack
yeah no use of ascending(s,i) here...
thanks ashita for the correction
reverse(stack *s){
IsEmpty(s)
return;
top = pop(s);
reverse(s);
ascending(s, top);
}
ascending(stack *s, int top){
IsEmpty(s){
push(top);
return;
}
i = pop(s);
if(i top){
@Srinivas:
shouldn't it be:
i = pop(s);
if(i top){
ascending(s, i);
push(top);
}
else{
ascending(s, top);
push(i);
}
On Sat, Sep 11, 2010 at 6:52 PM, ashita dadlani ash@gmail.com wrote:
else{
ascending(s, i);
push(top);
}
}
@swinivas:why have you used
@Srinivas:
Sorry for the confusion.
But if we have a stack {5,8,3,4,2} with 5 as the last input,ie,top,
do we have to arrange the stack such that s={2,3,4,5,8}with 2 as the top?
if I am getting it correct,then shouldn't your algo be modified slightly as
follows:?
reverse(stack *s){
IsEmpty(s)
yeah thanks ashita...u r right..
reverse(stack *s){
IsEmpty(s)
return;
top = pop(s);
reverse(s);
ascending(s, top);
}
ascending(stack *s, int top){
IsEmpty(s){
push(top);
return;
}
i = pop(s);
if(i top){
ascending(s, top);
push(i);
}
else{
What about a quick sort O(log n)
void sort_stack(Stack *src, Stack *dst)
{
if(! src-IsEmpty() )
{
Stack smaller, larger;
int pivot = src-Pop();
while(! src-IsEmpty() )
{
int tmp = src-Pop();