We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Quick Sort

Quick sort is an efficient sorting algorithm that's widely used in production sorting implementations. Like merge sort, quick sort is a recursive divide and conquer algorithm.

Divide:

  • Select a pivot element that will preferably end up close to the center of the sorted pack
  • Move everything onto the "greater than" or "less than" side of the pivot
  • The pivot is now in its final position
  • Recursively repeat the operation on both sides of the pivot

Conquer:

  • The array is sorted after all elements have been through the pivot operation

Explainer Video

Click to play video

Pseudocode

  • Select a "pivot" element - We'll arbitrarily choose the last element in the list
  • Move through all the elements in the list and swap them around until all the numbers less than the pivot are on the left, and the numbers greater than the pivot are on the right
  • Move the pivot between the two sections where it belongs
  • Recursively repeat for both sections

Assignment

We now have two sorting algorithms on our LockedIn backend! It is a bit annoying to maintain both in the codebase. Quicksort is fast on large datasets just like merge sort, but is also lighter on memory usage. Let's use quick sort for both follower count and influencer revenue sorting!

Complete the quick_sort and partition functions according to the given algorithms.

The process is started with quick_sort(A, 0, len(A)-1).