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