File size: 1,306 Bytes
a2b4f78 | 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 |
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
|