From 12f8d7d8a334f59abef56e3a1b46e63479a3b2a5 Mon Sep 17 00:00:00 2001 From: Vishant Rathi <129350236+vishantrathi@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:00:26 +0530 Subject: [PATCH] Create Stack Sorting --- data_structures/stacks/Stack Sorting | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 data_structures/stacks/Stack Sorting diff --git a/data_structures/stacks/Stack Sorting b/data_structures/stacks/Stack Sorting new file mode 100644 index 000000000000..3ce63145be9f --- /dev/null +++ b/data_structures/stacks/Stack Sorting @@ -0,0 +1,45 @@ +class Stack: + def __init__(self): + self.items = [] + + def push(self, item): + self.items.append(item) + + def pop(self): + if not self.is_empty(): + return self.items.pop() + return None + + def is_empty(self): + return len(self.items) == 0 + + def peek(self): + if not self.is_empty(): + return self.items[-1] + return None + + def size(self): + return len(self.items) + + def __str__(self): + return str(self.items) + +def sort_stack(original_stack): + aux_stack = Stack() + + while not original_stack.is_empty(): + # Pop the top element from the original stack + temp = original_stack.pop() + + # While the auxiliary stack is not empty and the top element + # of the auxiliary stack is greater than temp + while not aux_stack.is_empty() and aux_stack.peek() > temp: + original_stack.push(aux_stack.pop()) + + # Push temp in the auxiliary stack + aux_stack.push(temp) + + # Transfer elements back to the original stack + while not aux_stack.is_empty(): + original_stack.push(aux_stack.pop()) +