Choosing a Sorting Algorithm

Choosing a sorting algorithm: picking a sort by memory, stability, worst case and keys, the n log n limit on comparison sorts, and built-in sorts.

  • 8 cards
  • 4 questions
  • About 22 min
  • Intermediate to Advanced
Share

Choosing a sorting algorithm means matching a program's constraints to the sort that meets them, because no single sort is best on memory, stability, worst case and speed at once. The topic opens on a table of the earlier topics' sorts against those properties and five questions that turn it into a choice, then shows with a decision tree why no comparison sort can beat n log n. Two examples make the choice on real constraints: six orders sorted by two keys, which need a stable second sort, and a sensor buffer with no memory to spare, which leads to heapsort. A comparison card sets the built-in sorts of Python, Java and TypeScript side by side, a gotcha shows TypeScript sorting numbers as text, and an exercise picks the sort for four programs.

Contents

8 cardsnot started