Quicksort Python Implementation

Mike Sun
1 min readJun 20, 2020

--

Quick sort is really very confusing and the following implementation is merely one in many. Originally I wanted to write about quicksort from scratch but it dawned upon me that textual discussion simply cannot transcend the idea easily. Hence, I linked a Youtube video here to let you have a visual understanding. This seems to be best video online which uses in-place O(1) space complexity. The video explains the concept of using the last element as pivot and then recursively splits the array down till the base case of 1 element. I am impressed by this video because it tracks the recursion step by step, giving beginners a crystal clear explanation.

Disclaimer: Not my video!

Below is my python implementation based on the video’s concept.

The time complexity of this implementation of quicksort depends. In the worst case, whereby the initial array passed in is already sorted, the efficiency will be O(n²). This is because the pivot has to compare every single element in the array, similar to how bubblesort works. However, in average and best case, the efficiency will be O(nlogn). You can show this by simply making a table to see how number of comparison vs size of array grows.

There are other implementations out there which uses auxiliary arrays for quicksort, but that is usually not the case in textbooks. Other optimization of quicksort could include using a random element as pivot each time so as to not go into the worst case scenario.

Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mike Sun
Mike Sun

Written by Mike Sun

Random tech blog for my fellow peers troubleshooting stuff. Things I wished I knew without needing to spend hours/days digging...

No responses yet

Write a response