Question: 2. Sort elements in ascending order using stack, i.e. make the smallest element to be the top element of the stack. For example, given the

 2. Sort elements in ascending order using stack, i.e. make thesmallest element to be the top element of the stack. For example,

2. Sort elements in ascending order using stack, i.e. make the smallest element to be the top element of the stack. For example, given the stack elements (from bottom to top): 5, 2, 6, 9, sort the elements to make the stack elements become (from bottom to top): 9, 6, 5, 2. The only data structure you can use is array-based stack. Given a stack st, use one extra stack, tmpst, to store temporary data. a. Write a C++ program to sort elements in an array-based stack in ascending order. b. Demonstrate the sorting process with given input numbers (25, 10, 15, 20, 301.4 Algorithm: pop out the st to a variable tmp until it finishes all elements. while tmpst is not empty if the top element of tmpst is smaller than tmp, pop out tmpst and push that element onto st push tmp onto tmpst ......4 (1) push the elements 10,30,40 onto the stack st. (2) the process of sorting elements of stack st is: st: pop out 40=> tmpst: push 40. st: pop out 30=> tmpst: get the top element 40. Since 40>30 tmpst: pop out 40. st: push 40. tmpst: push 30. st: pop out 40=> tmpst: get the top element 30. Since 30 tmpst: get the top element 4e. Since 40>10 tmpst: pop out 40. st: push 40. tmpst: get the top element 30. Since 30>10 tmpst: pop out 30. st: push 30. tmpst: push 1e. st: pop out 30=> tmpst: get the top element 10. Since 18 tmpst: get the top element 30. Since 30

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!