From f0a25ff51e4444b2b305bee1d56c47522f10106a Mon Sep 17 00:00:00 2001 From: Suraj Kumar Modi <76468931+skmodi649@users.noreply.github.com> Date: Sun, 17 Oct 2021 20:03:18 +0530 Subject: [PATCH] Create StackSorting.java --- StackSorting.java | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 StackSorting.java diff --git a/StackSorting.java b/StackSorting.java new file mode 100644 index 0000000..49216f2 --- /dev/null +++ b/StackSorting.java @@ -0,0 +1,70 @@ +//This code sorts the Stack in such a way that greatest element is at the top of the stack + +/** Problem : Sort the Stack elements such that the greatest element is at the top of the stack + * Author : https://github.com/skmodi649 + */ + + + +import java.util.Scanner; +import java.util.Stack; + +class StackSorting{ + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + System.out.println("Enter the Stack size : "); + Stack s=new Stack<>(); + int n=sc.nextInt(); + System.out.println("Enter the elements in the Stack : "); + while(n-->0) + s.push(sc.nextInt()); + StackSorting g = new StackSorting(); + System.out.println("Sorted Stack : "); + Stack a = g.sort(s); //Sorted elements inserted in new Stack + while (!a.empty()) { + System.out.print(a.peek() + " "); + a.pop(); + } +} + + public void sortedInsert(Stack S , int element) // This method inserts the sorted element + { + if((S.empty()) || (S.peek() < element)) + S.push(element); + else + { + int temp = S.pop(); + sortedInsert(S , element); + S.push(temp); + } + } + public Stack sort(Stack s) //Sorts the stack and then returns it + { + if(!s.empty()) + { + int temp = s.pop(); + sort(s); + sortedInsert(s , temp); + } + return s; + } +} + + + +/** Expected Time Complexity: O(N*N) + * Expected Auxiliary Space Complexity : O(N) + * Constraints : 1<=N<=100 + */ + + +/** Test Case 1 : + * Enter the Stack size : 5 + * Enter the elements in the Stack : 0 1 2 3 9 + * Sorted Stack : 9 3 2 1 0 + * + * Test Case 2 : + * Enter the Stack size : 6 + * Enter the elements in the Stack : 3 1 2 9 8 7 + * Sorted Stack : 9 8 7 3 2 1 + */ \ No newline at end of file