File size: 735 Bytes
d25496d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | def quick_sort(S):
if len(S) <= 1:
return S
pivot = S[-1] # pick last element as pivot
left = [x for x in S[:-1] if x <= pivot]
right = [x for x in S[:-1] if x > pivot]
return quick_sort(left) + [pivot] + quick_sort(right)
# Main Program
while True:
n = int(input("Enter number of elements (greater than zero): "))
if n > 0:
S = [int(input(f"Enter element {i+1}: ")) for i in range(n)]
print("Elements before sorting:", S)
S = quick_sort(S)
print("Elements after sorting :", S)
else:
print("Invalid input!")
c = int(input("Press 1 to continue, 0 to exit: "))
if c != 1:
print("Program Terminated")
break
|