8000 add quick sort · AllAlgorithms/python@8823514 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8823514

Browse files
committed
add quick sort
1 parent d89e7d3 commit 8823514

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sorting/quick_sort.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import print_function
2+
3+
4+
def quick_sort(ARRAY):
5+
ARRAY_LENGTH = len(ARRAY)
6+
if( ARRAY_LENGTH <= 1):
7+
return ARRAY
8+
else:
9+
PIVOT = ARRAY[0]
10+
GREATER = [ element for element in ARRAY[1:] if element > PIVOT ]
11+
LESSER = [ element for element in ARRAY[1:] if element <= PIVOT ]
12+
return quick_sort(LESSER) + [PIVOT] + quick_sort(GREATER)
13+
14+
15+
if __name__ == '__main__':
16+
try:
17+
raw_input # Python 2
18+
except NameError:
19+
raw_input = input # Python 3
20+
21+
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
22+
unsorted = [ int(item) for item in user_input.split(',') ]
23+
print( quick_sort(unsorted) )

0 commit comments

Comments
 (0)
0