Quick Sort

Let's now study the famous Quick Sort algorithm!

Quick sort approach

Quick sort is the fastest-known, comparison-based sorting algorithm for lists in the average case.

Caveat: Merge sort works better on linked lists and there are other non-comparison-based algorithms that outperform Quick Sort.

Steps for quick sort algorithm

  1. Start with a list of n elements
  2. Choose a pivot element from the list to be sorted
  3. Partition the list into 2 unsorted sublists, such that all elements in one sublist are less than the pivot and all the elements in the other sublist are greater than the pivot
  4. Elements that are equal to the pivot can go in either sublist
  5. Sort each sublist recursively to yield two sorted sublists
  6. Concatenate the two sorted sublists and the pivot to yield one sorted list

How to pick the pivot?

There are two methods by which you can choose a pivot:

Choose randomly

The pivot can be picked randomly from the given elements. The chances of the first element being picked at every recursive call with this strategy is, 1n× ...

Access this course and 1400+ top-rated courses and projects.