Skip to content

Commit 8727246

Browse files
authored
Merge pull request TheAlgorithms#304 from hytae1993/master
quicksort_3_partition
2 parents 192ba07 + 31f968f commit 8727246

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sorts/quick_sort_3partition.py

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

0 commit comments

Comments
 (0)