Skip to content

Commit 117c476

Browse files
committed
add quick sort
1 parent 1fe9acb commit 117c476

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

ehco/quick_sort/sort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def quicksort(array):
2+
size = len(array)
3+
if not array or size < 2:
4+
return array
5+
pivot_idx = 0
6+
pivot = array[pivot_idx]
7+
less_part = [array[i] for i in range(size) if array[i] <= pivot and pivot_idx != i]
8+
great_part = [array[i] for i in range(size) if array[i] > pivot and pivot_idx != i]
9+
return quicksort(less_part) + [pivot] + quicksort(great_part)
10+
11+
12+
def test_quicksourt():
13+
import random
14+
15+
seq = list(range(10))
16+
random.shuffle(seq)
17+
assert quicksort(seq) == sorted(seq)
18+
19+
20+
if __name__ == "main":
21+
test_quicksourt()

0 commit comments

Comments
 (0)