Iteration

Sorting

Lesson

We talked about Sorted Arrays, but we didn't talk about how to create them, which is called "Sorting". You don't need to master sorting algorithms for an interview. To be safe, you might want to at least understand HeapSort and MergeSort.

\notag \\[18bp]

Time

Space

HeapSort

O(nlogn)O(n \log n)

O(1)O(1)

MergeSort

O(nlogn)O(n \log n)

O(n)O(n)

QuickSort

O(nlogn)O(n \log n)

O(1)O(1)

\notag \\[18bp]

Non-Comparison ➡️

Counting Sort

O(n+range)O(n +|\text{range}|)

O(n)O(n)

Radix Sort

O(ndigits)O(n \cdot |\text{digits}|)

O(n)O(n)

Sorting arbitrary items in general requires comparing elements with each other. You can prove this takes at least O(nlogn)O(n \log n) time. This applies to all comparison sort algorithms: HeapSort, MergeSort, and QuickSort.

Counting Sort and Radix Sort are not comparsion sort algorithms - they only work on integers and other discretely-spaced elements. That's why they're allowed to run faster, in O(n+range)O(n+|\text{range}|) and O(ndigits)O(n \cdot |\text{digits}|) time.

Python's built-in sorting functions .sort() and sorted() run "TimSort" in the background (a MergeSort variant).

Mark as Completed:
Submits:
test
Test your code to get an output here!