Q. Write a Python program to sort a Stack in ascending order without using an additional Stack.

Answer =

stack = eval(input("Enter a stack :-"))

for i in range (len(stack)):
    for j in range(len(stack)-1):
        if stack [ j ]  > stack [ j + 1 ] :
            stack [ j ] , stack [ j + 1 ] = stack [ j + 1 ] , stack [ j ]

print(stack)

Output :-

Enter a stack :-[21,88.6,457,1,659,457,21,99]
[1, 21, 21, 88.6, 99, 457, 457, 659]

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post