Question

In: Computer Science

For exam review: Given a stack S1 with n numbers and an empty stack S2, design...

For exam review:

Given a stack S1 with n numbers and an empty stack S2, design an algorithm (and write psudeocode for it) to sort all the numbers (from small on top to large on bottom) and store them in the originally empty stack S2, using only the stack ADT functions with the two given stacks, and a fixed number (independent of n) of int and char variables. What is the time complexity of your algorithm (related to n) in the BEST/WORST case?

Solutions

Expert Solution

The worst case time-complexity is O(n).

Code:

#include <bits/stdc++.h>
using namespace std;


// method to sort stack
stack<int> sort(stack<int> &st1)
{
   stack<int> st2;

   while (!st1.empty())
   {
  
       int tmp = st1.top();
       st1.pop();

   // untill st2 is not empty and top is larger than temp value
       while (!st2.empty() && st2.top() > tmp)
       {
      
       //pop from st2 and push to st1
           st1.push(st2.top());
           st2.pop();
       }

  
       st2.push(tmp);
   }

   return st2;
}


int main()
{
// main method to test sorting of stack
// intialize stack 1 and push elements to it
   stack<int> st1;
   st1.push(7);
   st1.push(3);
   st1.push(4);
   st1.push(9);
   st1.push(2);
   st1.push(5);

   // call method to sort stack and store return value in another stack
   stack<int> st2 = sort(st1);
   cout << "Sorted stack values are:\n";

   while (!st2.empty())
   {
   // display the values
       cout << st2.top()<< " ";
       st2.pop();
   }
}

Screenshots:

Output:


Related Solutions

Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.
Let S = (s1, s2, . . . , sn) be a given sequence of integer...
Let S = (s1, s2, . . . , sn) be a given sequence of integer numbers. The numbers can be positive or negative. We define a slice of S as a sub- sequence (si,si+1,...,sj) where 1 ≤ i < j ≤ n. The weight of a slice is defined as the sum of its elements. Provide efficient algorithms to answer each of the following questions: a)Is there any slice with zero weight ? b)Find the maximum weight slice in...
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
Given that dE = CvdT = TdS - PdV prove S2 - S1 = Cv*ln(T2 /...
Given that dE = CvdT = TdS - PdV prove S2 - S1 = Cv*ln(T2 / T1) + R*ln( V2 / V1) for the Ideal gas.
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
Write the basic feasible solution from the tableau given. x1 x2 x3 s1 s2 z 8...
Write the basic feasible solution from the tableau given. x1 x2 x3 s1 s2 z 8 6 −1 1 0 0 160 5 2 4 0 1 0 148 −6 −10 −5 0 0 1 146
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT