Upload 7.py
Browse files
7.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def inplace_quick_sort(S, a, b):
|
| 3 |
+
if a >= b:
|
| 4 |
+
return
|
| 5 |
+
pivot = S[b]
|
| 6 |
+
left = a
|
| 7 |
+
right = b - 1
|
| 8 |
+
while left <= right:
|
| 9 |
+
while left <= right and S[left] < pivot:
|
| 10 |
+
left += 1
|
| 11 |
+
while left <= right and S[right] > pivot:
|
| 12 |
+
right -= 1
|
| 13 |
+
if left <= right:
|
| 14 |
+
S[left], S[right] = S[right], S[left]
|
| 15 |
+
left += 1
|
| 16 |
+
right -= 1
|
| 17 |
+
|
| 18 |
+
S[left], S[b] = S[b], S[left]
|
| 19 |
+
inplace_quick_sort(S, a, left - 1)
|
| 20 |
+
inplace_quick_sort(S, left + 1, b)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
while True:
|
| 24 |
+
S = []
|
| 25 |
+
print("\tImplementation of In-place Quick-Sort algorithm using List")
|
| 26 |
+
|
| 27 |
+
n = int(input("\nEnter number of elements (greater than Zero): "))
|
| 28 |
+
if n >= 1:
|
| 29 |
+
print("Enter the elements one by one:")
|
| 30 |
+
for i in range(n):
|
| 31 |
+
S.append(int(input()))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
print("\nOutput")
|
| 36 |
+
print("-------")
|
| 37 |
+
print("\nElements before sorting:")
|
| 38 |
+
print(S)
|
| 39 |
+
|
| 40 |
+
inplace_quick_sort(S, 0, len(S) - 1)
|
| 41 |
+
|
| 42 |
+
print("\nElements after sorting:")
|
| 43 |
+
print(S)
|
| 44 |
+
else:
|
| 45 |
+
print("Invalid Input!")
|
| 46 |
+
|
| 47 |
+
c = int(input("\nTo continue press 1 otherwise press 0... "))
|
| 48 |
+
if c != 1:
|
| 49 |
+
print("Program Terminated")
|
| 50 |
+
break
|