Quicksort (Hoare)

Published

2023-08-05

The quicksort algorithm lives up to its name. It is the fastest, general-purpose sorting algorithm we have for a wide variety of use cases. It is a divide-and-conquer algorithm that works by choosing an element, called the “pivot” (or “divider” in the textbook), and then partitions elements by placing values less than that of the pivot to the left of the pivot, and values greater than or equal to that of the pivot to the right of the pivot.

At this point, I encourage you to try implementing quicksort yourself. You won’t be graded on this, but it’s worth the effort. Once you’ve had a go at it, have a look at this video. Here we implement quicksort in C++, and introduce function overloading.

Here we implement a stable version of quicksort in C++. Notice that we use vectors to hold intermediate results, thereby preserving the relative order of identical inputs. Again, feel free to give this a try on your own before watching the video.

Resources and supplemental materials:

Comprehension check:

  1. True or false. Quicksort is a divide-and-conquer algorithm.
  2. The average case time complexity of quicksort is ____________.
  3. Quicksort recursively partitions elements, placing them to one side or the other of the _________.
  4. Quicksort can be implemented to be _____________ at the expense of an increase in space complexity.

Answers: ǝlqɐʇs / (ɹǝpᴉʌᴉp ɹo) ʇoʌᴉd / (u ƃol u) O / ǝnɹʇ

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.