This 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