I was given a task to sort numbers in stack a
of integers in ascending order using two stacks a
and b
.
Using eleven operations:
swap a
- swap the first 2 elements at the top of stack a
swap b
- swap the first 2 elements at the top of stack b
.sa
and sb
at the same time.push a
- take the first element at the top of b
and put it at the top of a
.push b
- take the first element at the top of a
and put it at the top of b
.rotate a
- shift up all elements of stack a
by 1. The first element becomes
the last one.rotate b
- shift up all elements of stack b
by 1. The first element becomes
the last one.ra
and rb
at the same time.rotate a
- shift down all elements of stack a
by 1. The last element
becomes the first one.rotate b
- shift down all elements of stack b
by 1. The last element
becomes the first one.rra
and rrb
at the same time.My sorting function
void sorts_stack(stack *a, stack *b)
{
int srt;
srt = is_not_sorted(a);
if (srt)
{
if (a->list[srt] == top(a) && a->list[srt] > a->list[0])
{
rotate_ra_rb(a->list, a->size); //ra : rotate a
putstr("ra\n");
}
else if (a->list[srt] == top(a) && a->list[srt] > a->list[srt - 1])
{
swap_sa_sb(a->list, a->size);//sa : swap a
putstr("sa\n");
}
else if (a->list[srt] > a->list[srt - 1])
{
putstr("pb\n"); //pb : push b
push_pb(a, b);
}
sorts_stack(a, b);
}
else if (b->size > 0)
{
if (top(a) < top(b))
{
push_pa(a, b); //pa : push a
putstr("pa\n");
}
else if ((top(a) > top(b)) && b->size != 0)
{
push_pa(a, b); //pa : push a
putstr("pa\n");
}
sorts_stack(a, b);
}
}
my function sort the stack, I think it takes too many steps to sort. I need suggestions or advice on how to make it sort the stack with less steps taken. complete online code
Understanding the ProblemYou could use another stack if needed. The top of the stack must point to the smallest element. The stack must be sorted in increasing order from the top of the stack to its bottom.
The steps to sort a queue using Stack are: Create an auxiliary stack and an auxiliary queue. If the stack is empty, push the element in the stack. If the next element at the front of the queue is greater than the top of the stack, push it on the stack.
Push all elements of array in 1st stack 2. Run a loop for 'n' times(n is size of array) having the following : 2. a. Keep on pushing elements in the 2nd stack till the top of second stack is smaller than element being pushed from 1st stack.
Given two stacks A and B, where A is filled with a random permutation of elements and B is empty, and one temporary variable T able to hold one element (and a counter but the counter doesn't count) you can sort A in ascending order into B by:
You can (and should) put all of it in a single loop, of course.
This is really a two queue problem, since the rotate operations effectively turn a stack into a queue. In this case a bottom up merge sort can be performed, which would be relatively fast.
Using a linked list (instead of an array) to implement the stack / queue would speed up the rotates.
Bottom up merge sort for 2 queues:
Initial split: Pop elements off the original queue, and appended elements alternately to the original and temporary queue. Set queue size to number of elements popped off original queue. Set sorted run size to 1.
Merge pass: using sorted run size to determine size of runs, merge runs from each queue, alternating the merged output between the original and temporary queues. Double the run size, stop if run size >= queue size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With