Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is my code in Java: Please tell me what is the running time complexity! public class ProblemOne { // created stack called input public

image text in transcribedThis is my code in Java:

Please tell me what is the running time complexity!

public class ProblemOne { // created stack called "input" public static Stack sort(Stack input) { // created stack called "tempStack" Stack tempStack = new Stack(); // "while" loop that organizes the stack in descending order while (!input.isEmpty()) { int tmp = input.pop(); while (!tempStack.isEmpty() && tempStack.peek() > tmp) { input.push(tempStack.pop()); } tempStack.push(tmp); } return tempStack; } //main method begins public static void main(String[] args) { { // create stack called "input" Stack input = new Stack(); // add values to the stack input.add(50); input.add(2); input.add(28); input.add(98); input.add(78); input.add(34); // print stack before sorting System.out.println("Stack before sorting: " + input); System.out.println(); // create stack called "tempStack" Stack tempStack = sort(input); // print stack after sorting System.out.print("Stack after sorting: ["); // organize the stack in descending order while (!tempStack.empty()) { System.out.print(tempStack.pop() + " "); } System.out.print("]"); } } }

(a) Write a method in Java to sort a stack of n integer numbers, s, in descending order. static Stack sort(Stack s) To implement this method you must use one more auxiliary stack and you should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push, pop, peek, and isEmpty. (b) What is the running time complexity of your method? Justify

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions

Question

Understanding Groups

Answered: 1 week ago