Catalog / Sorting Algorithms Cheat Sheet
Sorting Algorithms Cheat Sheet
A concise cheat sheet covering common sorting algorithms, their time complexities, and pseudocode for quick reference during coding interviews and algorithm analysis.
Basic Sorting Algorithms
Bubble Sort
Description: |
Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. |
Time Complexity: |
Worst/Avg: O(n^2), Best: O(n) (when nearly sorted) |
Space Complexity: |
O(1) |
Pseudocode: |
|
Use Cases: |
Rarely used in practice due to its inefficiency on large datasets. Good for small, nearly sorted datasets. |
Selection Sort
Description: |
Finds the minimum element in each iteration and places it at the beginning. |
Time Complexity: |
O(n^2) (always) |
Space Complexity: |
O(1) |
Pseudocode: |
|
Use Cases: |
Simple to implement but generally inefficient for large datasets. Performs well compared to bubble sort. |
Insertion Sort
Description: |
Builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. |
Time Complexity: |
Worst/Avg: O(n^2), Best: O(n) (when nearly sorted) |
Space Complexity: |
O(1) |
Pseudocode: |
|
Use Cases: |
Efficient for small datasets or nearly sorted data. Often used as a subroutine in more complex sorting algorithms. |
Divide and Conquer Sorting
Merge Sort
Description: |
Divides the array into halves, recursively sorts each half, and then merges the sorted halves. |
Time Complexity: |
O(n log n) (always) |
Space Complexity: |
O(n) |
Pseudocode: |
|
Use Cases: |
Guaranteed O(n log n) performance, suitable for large datasets. Used in external sorting. |
Quick Sort
Description: |
Picks an element as a pivot and partitions the array around the pivot. Average case is very efficient. |
Time Complexity: |
Worst: O(n^2), Avg: O(n log n), Best: O(n log n) |
Space Complexity: |
O(log n) average, O(n) worst (due to recursion stack) |
Pseudocode: |
|
Use Cases: |
Generally the fastest sorting algorithm in practice. Sensitive to pivot selection. |
Advanced Sorting Algorithms
Heap Sort
Description: |
Uses a binary heap data structure to sort the array. In-place algorithm. |
Time Complexity: |
O(n log n) (always) |
Space Complexity: |
O(1) |
Pseudocode: |
|
Use Cases: |
Guaranteed O(n log n) performance, in-place, but generally slower than quicksort in practice. |
Radix Sort
Description: |
Sorts integers by processing individual digits. Non-comparison based sorting. |
Time Complexity: |
O(nk) where k is the number of digits in the largest number. |
Space Complexity: |
O(n+k) |
Pseudocode: |
|
Use Cases: |
Efficient for integers when the range of digits is known. Can be faster than comparison sorts under certain conditions. |
Sorting Algorithm Summary
Time and Space Complexity Comparison
|
Choosing the Right Sorting Algorithm
|