Skip to content

Commit a7c77fa

Browse files
authored
Create quicksort.py
Quick and dirty implementation of a quicksort algorithm
1 parent 334a054 commit a7c77fa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

allalgorithms/sorting/quicksort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def partition(xs, start, end):
2+
follower = leader = start
3+
while leader < end:
4+
if xs[leader] <= xs[end]:
5+
xs[follower], xs[leader] = xs[leader], xs[follower]
6+
follower += 1
7+
leader += 1
8+
xs[follower], xs[end] = xs[end], xs[follower]
9+
return follower
10+
11+
def _quicksort(xs, start, end):
12+
if start >= end:
13+
return
14+
p = partition(xs, start, end)
15+
_quicksort(xs, start, p-1)
16+
_quicksort(xs, p+1, end)
17+
18+
def quicksort(xs):
19+
_quicksort(xs, 0, len(xs)-1)
20+
21+
# To use: create a list and send it to quicksort: quicksort(list placed here)

0 commit comments

Comments
 (0)