Master fundamental algorithms for organizing and finding data
Section 2 of 5
From simple quadratic sorts to efficient divide-and-conquer techniques
Sorting is one of the most fundamental operations in computer science. We'll explore algorithms ranging from simple O(n²) methods suitable for small datasets to sophisticated O(n log n) techniques that power modern systems.
| Algorithm | Best | Average | Worst | Space | Stable | Notes |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | ✓ | Simple, rarely used |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | ✗ | Min swaps |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | ✓ | Adaptive, online |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | ✓ | Stable, predictable |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | ✗ | In-place, cache-friendly |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | ✗ | In-place, guaranteed |
Repeatedly step through the list, compare adjacent pairs, and swap if they're in the wrong order. Larger elements "bubble up" to the end.
How it works:
1function bubbleSort(arr):2 n = arr.length3 for i from 0 to n-1:4 swapped = false5 for j from 0 to n-i-2:6 if arr[j] > arr[j+1]:7 swap(arr[j], arr[j+1])8 swapped = true9 if not swapped:10 break // Already sorted11 return arrBest: O(n)
Already sorted, early exit
Avg: O(n²)
n(n-1)/2 comparisons
Worst: O(n²)
Reverse sorted
Find the minimum element from unsorted part and put it at the beginning. Repeat for remaining unsorted part.
1function selectionSort(arr):2 n = arr.length3 for i from 0 to n-1:4 minIndex = i5 for j from i+1 to n-1:6 if arr[j] < arr[minIndex]:7 minIndex = j8 if minIndex != i:9 swap(arr[i], arr[minIndex])10 return arrKey Insight:
Selection sort performs the minimum number of swaps (n-1 max) among all sorting algorithms, making it useful when write operations are expensive (e.g., EEPROM, flash memory).
Build sorted array one item at a time by inserting each element into its correct position. Like sorting playing cards in your hand.
1function insertionSort(arr):2 for i from 1 to n-1:3 key = arr[i]4 j = i - 15 // Shift elements greater than key to right6 while j >= 0 and arr[j] > key:7 arr[j+1] = arr[j]8 j = j - 19 arr[j+1] = key10 return arrWhy It's Special:
Divide and Conquer: Split array in half recursively, sort each half, then merge sorted halves.
1function mergeSort(arr, left, right):2 if left >= right:3 return4 5 mid = (left + right) / 26 mergeSort(arr, left, mid)7 mergeSort(arr, mid+1, right)8 merge(arr, left, mid, right)9 10function merge(arr, left, mid, right):11 // Create temp arrays12 L = arr[left...mid]13 R = arr[mid+1...right]14 15 i = 0, j = 0, k = left16 while i < L.length and j < R.length:17 if L[i] <= R[j]:18 arr[k++] = L[i++]19 else:20 arr[k++] = R[j++]21 22 // Copy remaining23 while i < L.length: arr[k++] = L[i++]24 while j < R.length: arr[k++] = R[j++]Complexity Analysis:
Divide and Conquer: Pick a pivot, partition array so elements < pivot are left, elements > pivot are right. Recursively sort partitions.
1function quickSort(arr, low, high):2 if low < high:3 pivotIndex = partition(arr, low, high)4 quickSort(arr, low, pivotIndex - 1)5 quickSort(arr, pivotIndex + 1, high)6 7function partition(arr, low, high):8 pivot = arr[high] // Choose last element as pivot9 i = low - 1 // Index of smaller element10 11 for j from low to high-1:12 if arr[j] < pivot:13 i++14 swap(arr[i], arr[j])15 16 swap(arr[i+1], arr[high])17 return i + 1Pivot Selection Strategies:
Why Quick Sort is Fast in Practice:
Build a max-heap from the array, then repeatedly extract the maximum and rebuild the heap.
1function heapSort(arr):2 n = arr.length3 4 // Build max heap5 for i from n/2 - 1 down to 0:6 heapify(arr, n, i)7 8 // Extract elements one by one9 for i from n-1 down to 1:10 swap(arr[0], arr[i]) // Move max to end11 heapify(arr, i, 0) // Heapify reduced heap12 13function heapify(arr, n, i):14 largest = i15 left = 2*i + 116 right = 2*i + 217 18 if left < n and arr[left] > arr[largest]:19 largest = left20 if right < n and arr[right] > arr[largest]:21 largest = right22 23 if largest != i:24 swap(arr[i], arr[largest])25 heapify(arr, n, largest)Heap Sort Characteristics:
Approximate comparisons for sorting 1,000,000 elements: