diff --git a/allalgorithms/sorting/quicksort.py b/allalgorithms/sorting/quicksort.py new file mode 100644 index 0000000..6990462 --- /dev/null +++ b/allalgorithms/sorting/quicksort.py @@ -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) diff --git a/docs/sorting/quicksort.md b/docs/sorting/quicksort.md new file mode 100644 index 0000000..e09bc38 --- /dev/null +++ b/docs/sorting/quicksort.md @@ -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