Python / 7.py
Madhan-Alagarsamy's picture
Upload 7.py
a2b4f78 verified
def inplace_quick_sort(S, a, b):
if a >= b:
return
pivot = S[b]
left = a
right = b - 1
while left <= right:
while left <= right and S[left] < pivot:
left += 1
while left <= right and S[right] > pivot:
right -= 1
if left <= right:
S[left], S[right] = S[right], S[left]
left += 1
right -= 1
S[left], S[b] = S[b], S[left]
inplace_quick_sort(S, a, left - 1)
inplace_quick_sort(S, left + 1, b)
while True:
S = []
print("\tImplementation of In-place Quick-Sort algorithm using List")
n = int(input("\nEnter number of elements (greater than Zero): "))
if n >= 1:
print("Enter the elements one by one:")
for i in range(n):
S.append(int(input()))
print("\nOutput")
print("-------")
print("\nElements before sorting:")
print(S)
inplace_quick_sort(S, 0, len(S) - 1)
print("\nElements after sorting:")
print(S)
else:
print("Invalid Input!")
c = int(input("\nTo continue press 1 otherwise press 0... "))
if c != 1:
print("Program Terminated")
break