basecs
Published in

basecs

Pivoting To Understand Quicksort [Part 1]

Pivoting to understand quicksort

A quick study on quicksort basics

Quicksort: a definition
  1. First, quicksort determines something called a pivot, which is a somewhat arbitrary element in the collection.
  2. Next, using the pivot point, it partitions (or divides) the larger unsorted collection into two, smaller lists. It uses some smart logic to decide how to do the partitioning: it moves all the elements smaller than the pivot to the left (before) the pivot element, and moves all the elements larger than the pivot to the right (after) the pivot element.
How quicksort works

Even though the list isn’t completely sorted yet, we know that the items are in the correct order in relation to the pivot. This means that we never have to compare elements on the left side of the partition to elements on the right side of the partition. We already know they are in their correct spots in relation to the pivot.

What we choose as a pivot is important!

Making quick work of quicksort

Quick sort in action: part 1.
Quick sort in action: part 2
Quick sort in action: part 3

Quick-fire swapping

Digging deeper into sorting

If the quicksort algorithm determines that two elements are out of order, it leans on its references to swap them into their correct place in the collection.

A guide to implementing quicksort

However — if both the left reference is greater than the pivot and the right reference is smaller than the pivot, we know that we’ve stumbled upon two elements that are out of order.

How quicksort swaps: part 1
How quicksort swaps: part 2
How quicksort swaps: part 3

Resources

  1. Quicksort algorithm, mycodeschool
  2. Sorting Algorithms: QuickSort, Professor Lydia Sinapova
  3. Quick Sort, Computerphile
  4. Data Structure and Algorithms — Quick Sort, Tutorialspoint
  5. QuickSort, GeeksForGeeks
  6. Quick Sort in 4 Minutes, Michael Sambol

--

--

Exploring the basics of computer science, every Monday, for a year.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store