This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
DSA/midterm_prep/184-1.py

48 lines
995 B
Python
Raw Normal View History

2019-04-23 11:35:05 +00:00
#!/usr/bin/env python3
import sys
import math
def three_way_partition(A, begin, end):
i = 0
j = end - 2
p = end - 1
while i < j:
print(A[begin:i], A[i:j+1], A[j+1:p], A[p:end])
if A[i] == A[p]:
p = p - 1
A[i], A[p] = A[p], A[i]
if j == p:
j = j - 1
elif A[j] == A[p]:
p = p - 1
A[j], A[p] = A[p], A[j]
if j == p:
j = j - 1
elif A[i] < A[p]:
i = i + 1
elif A[j] > A[p]:
j = j - 1
else:
A[i], A[j] = A[j], A[i]
m = end - 1
while i < p:
print(A)
A[i], A[m] = A[m], A[i]
m = m - 1
i = i + 1
return [j, m + 1]
if __name__ == "__main__":
A = []
for line in sys.stdin:
line = line.strip()
if line == '' or line == '\n':
break
A.append(int(line))
print(three_way_partition(A, 0, len(A)))
print(A)