Quicksort in Python. Introduction to ‘quickSort’ in Python | by Sadrach Pierre, Ph.D. | Towards Data Science

Sorting algorithms are among the most important algorithms in computer science. Sorting involves the task of ordering elements in a list or array. The most common uses of ordering are numerical and lexicographical. In this post, we will discuss how to implement a ‘quickSort’ algorithm in python which we will use to numerically sort a list.
Let’s get started!
The process fundamental to the ‘QuickSort’ algorithm is the partition. The way partition works is by first selecting a pivot. Options for pivots include:
First elementLast elementRandom elementMiddle element
First element
Last element
Random element
Middle element
Upon selecting the pivot, we partition the elements of the array based on the pivot value. Namely, for an input array and pivot, we put all elements smaller than the pivot before the pivot and all elements greater than the pivot after it. To see this, let’s consider the following list:
Let’s select the last element, ‘3’, as our pivot:
Next, we create two sublists, the left list contains values less than the pivot and the right sublist contains values greater than the sublist:
We then find the pivot for the sublists and repeat the partition process. The pivot for the left sublist is 1 and the pivot for the right sublist is 6:
Next, we partition our sublists around their pivots:
And finally let’s select the pivots for the last set of sublists, -6 on the left and 8 on the right:
And let’s complete the final partition:
And we end up with the following sorted list:
Let’s see how to implement this algorithm in python. To start, let’s define our partition function. Our function will take a list, a ‘low’ variable and a ‘high’ variable. ‘Low’ is the index of the smaller element (where we’ll start in the list) and ‘high’ is the index of the pivot, which will be the last element. We initialize the index of the smallest element to be ‘low’ -1 and we define the pivot to be the value of the last element:
def partition(list,low,high):    i = (low - 1)    pivot = array[high]
Next, we will write a ‘for-loop’ iterating from ‘low’ to ‘high’:
def partition(list,low,high):    i = (low - 1)    pivot = array[high]    for j in range(low, high):
Next, we check if the value at the current index within the ‘for-loop’ is less than or equal to the pivot. If the current value is less than the pivot, we increment the index of the smaller element:
def partition(input_list,low,high):    i = (low - 1)    pivot = input_list[high]    for j in range(low, high):        if input_list[j] <= pivot:            i = i + 1            input_list[i], input_list[j] =  input_list[j], input_list[i]
Otherwise, if the current value is greater than the pivot, we swap the greater value with the pivot. We also return the index:
def partition(input_list,low,high):    i = (low - 1)    pivot = input_list[high]    for j in range(low, high):        if input_list[j] <= pivot:            i = i + 1            input_list[i], input_list[j] =  input_list[j], input_list[i]    input_list[i+1],input_list[high] = input_list[high],input_list[i+1]    return (i+1)
Next, let’s define our ‘quickSort’ function. We first check if the value for ‘low’ is less than ‘high’ and if the condition is true we define the partitioning index:
def quickSort(input_list, low, high):    if low < high:        partition_index = partition(input_list,low,high)
We then recursively call ‘quickSort’ and to sort elements before the partition:
def quickSort(input_list, low, high):    if low < high:        partition_index = partition(input_list,low,high)        quickSort(input_list, low, partition_index - 1)
and sort elements after the partition:
def quickSort(input_list, low, high):    if low < high:        partition_index = partition(input_list,low,high)        quickSort(input_list, low, partition_index - 1)        quickSort(input_list, partition_index + 1, high)
We can now test our ‘quickSort’ function. Let’s test it with the list we used in our original example:
input_l = [9, -3, 5, 2, 6, 8, -6, 1, 3]
Let’s define our ‘high’ variable which will be the length of the list:
list_length = len(input_l)
The ‘low’ variable will be 0, since lists are zero indexed in python. Let’s now call the ‘quickSort’ method with our list, our ‘low’ value, which is 0, and ‘list_length’ - 1:
quickSort(input_l, 0, list_length -1)
Now our array should be sorted. Let’s print our array:
print(input_l)
We see that our function successfully sorted our list. I’ll stop here but I encourage you to play around with the code yourself.
To summarize, in this post we discussed the quick sort algorithm and showed how to implement it in python. If you are interested in learning more about sorting algorithms, I encourage you to explore realpython.com which goes into other sorting algorithms such as ‘merge sort’, ‘insertion sort’ and ‘bubble sort’. I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!