47 lines
995 B
Python
Executable file
47 lines
995 B
Python
Executable file
#!/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)
|