Skip to content

Commit 4305a29

Browse files
authored
Merge pull request abranhe#25 from BubbaBeans/patch-1
Create quicksort.py
2 parents 1192952 + 454cb10 commit 4305a29

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

allalgorithms/sorting/quicksort.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Quick Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Brian D. Hopper
7+
# Github: @bubbabeans
8+
#
9+
def partition(xs, start, end):
10+
follower = leader = start
11+
while leader < end:
12+
if xs[leader] <= xs[end]:
13+
xs[follower], xs[leader] = xs[leader], xs[follower]
14+
follower += 1
15+
leader += 1
16+
xs[follower], xs[end] = xs[end], xs[follower]
17+
return follower
18+
19+
def _quicksort(xs, start, end):
20+
if start >= end:
21+
return
22+
p = partition(xs, start, end)
23+
_quicksort(xs, start, p-1)
24+
_quicksort(xs, p+1, end)
25+
26+
def quicksort(xs):
27+
_quicksort(xs, 0, len(xs)-1)
28+
29+
# To use: create a list and send it to quicksort: quicksort(list placed here)

docs/sorting/quicksort.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Quicksort
2+
3+
A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point.
4+
5+
A pivot point is created in the middle of an array, and all larger items go after the pivot point, and smaller items are placed in front
6+
of the pivot point.
7+
8+
The pivot point is then moved to the middle of either the smaller or larger items, and the sort is run again on that half.
9+
10+
This continues over and over again until everything is in the proper place.
11+
12+
## Install
13+
14+
```
15+
pip install allalgorithms
16+
```
17+
18+
## Usage
19+
20+
```py
21+
from allalgorithms.sorting import quicksort
22+
23+
arr = [77, 2, 10, -2, 1, 7]
24+
25+
print(quicksort(arr))
26+
# -> [-2, 1, 2, 7, 10, 77]
27+
```
28+
29+
## API
30+
31+
```
32+
quicksort(array)
33+
```
34+
35+
> Returns a sorted array
36+
37+
##### Params:
38+
39+
- `array`: Sorted Array

0 commit comments

Comments
 (0)