I am totally new to python and I am trying to implement quicksort in it. Could someone please help me complete my code?
I do not know how to concatenate the three arrays and printing them.
def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
if x == pivot:
equal.append(x)
if x > pivot:
greater.append(x)
sort(less)
sort(pivot)
sort(greater)
The algorithm has 4 simple steps:
Code for the algorithm in python:
Carry on with this algorithm recursively with the left and right parts.
I am attaching the code below! This quicksort is a great learning tool because of the Location of the pivot value. Since it is in a constant place, you can walk through it multiple times and really get a hang of how it all works. In practice it is best to randomize the pivot to avoid O(N^2) runtime.
functional approach:
Another quicksort implementation: