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...
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
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).
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +. (b) Analyze time and space of part (a) (c)Implement part (a) in your favorite programming language without using 0+0 operator. You should read the two numeric strings from a FILE input.in and output their addition to standard output. You Solution will be test on 10,000 test cases. Each test case will include...
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.
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT