Skip to content

Commit ca7eb46

Browse files
author
Hyuntae
committed
quicksort_3_partition
1 parent 2e74c8e commit ca7eb46

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

sorts/quick_sort_3partition.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import print_function
2+
3+
def quick_sort_3partition(sorting, left, right):
4+
5+
if right <= left:
6+
return
7+
a = left
8+
b = right
9+
pivot = sorting[left]
10+
i = left
11+
while i <= b:
12+
if sorting[i] < pivot:
13+
sorting[a], sorting[i] = sorting[i], sorting[a]
14+
a += 1
15+
i += 1
16+
elif sorting[i] > pivot:
17+
sorting[b], sorting[i] = sorting[i], sorting[b]
18+
b -= 1
19+
else:
20+
i += 1
21+
quick_sort_3partition(sorting, left, a - 1)
22+
quick_sort_3partition(sorting, b + 1, right)
23+
24+
if __name__ == '__main__':
25+
try:
26+
raw_input # Python 2
27+
except NameError:
28+
raw_input = input # Python 3
29+
30+
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
31+
unsorted = [ int(item) for item in user_input.split(',') ]
32+
quick_sort_3partition(unsorted,0,len(unsorted)-1)
33+
print(unsorted)

0 commit comments

Comments
 (0)