Skip to content

Create quicksort.py #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions allalgorithms/sorting/quicksort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: UTF-8 -*-
#
# Quick Sort Algorithm
# The All ▲lgorithms library for python
#
# Contributed by: Brian D. Hopper
# Github: @bubbabeans
#
def partition(xs, start, end):
follower = leader = start
while leader < end:
if xs[leader] <= xs[end]:
xs[follower], xs[leader] = xs[leader], xs[follower]
follower += 1
leader += 1
xs[follower], xs[end] = xs[end], xs[follower]
return follower

def _quicksort(xs, start, end):
if start >= end:
return
p = partition(xs, start, end)
_quicksort(xs, start, p-1)
_quicksort(xs, p+1, end)

def quicksort(xs):
_quicksort(xs, 0, len(xs)-1)

# To use: create a list and send it to quicksort: quicksort(list placed here)
39 changes: 39 additions & 0 deletions docs/sorting/quicksort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Quicksort

A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point.

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
of the pivot point.

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.

This continues over and over again until everything is in the proper place.

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.sorting import quicksort

arr = [77, 2, 10, -2, 1, 7]

print(quicksort(arr))
# -> [-2, 1, 2, 7, 10, 77]
```

## API

```
quicksort(array)
```

> Returns a sorted array

##### Params:

- `array`: Sorted Array