-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_a_stack_using_recursion.py
60 lines (44 loc) · 1.49 KB
/
sort_a_stack_using_recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Given a stack, the task is to sort it such that the top of the stack has the greatest element.
Example 1:
Input:
Stack: 3 2 1
Output: 3 2 1
Example 2:
Input:
Stack: 11 2 32 3 41
Output: 41 32 11 3 2
Your Task:
You don't have to read input or print anything. Your task is to complete the function sort() which sorts the elements present in the given stack. (The sorted stack is printed by the driver's code by popping the elements of the stack.)
Expected Time Complexity: O(N*N)
Expected Auxilliary Space: O(N) recursive.
Constraints:
1<=N<=100
"""
class Solution:
# your task is to complete this function
# function sort the stack such that top element is max
# funciton should return nothing
# s is a stack
# Code here
def Sorted(self, s):
# Base case: if the stack is empty, return
if len(s) == 0:
return
# Remove the top element
top = s.pop()
# Sort the remaining stack
self.Sorted(s)
# Insert the removed element back into the sorted stack
self.sorted_insert(s, top)
def sorted_insert(self, s, element):
# If stack is empty or top of stack is less than or equal to element, push the element
if len(s) == 0 or s[-1] <= element:
s.append(element)
return
# Remove the top element
top = s.pop()
# Recur for the rest of the stack
self.sorted_insert(s, element)
# Put back the top element
s.append(top)