Sorting Algorithms
Bubble Sort Space Complexity
O(1) Space Complexity
Heap Sort Space Complexity
O(1) Space Complexity
Insertion Sort Space Complexity
O(1) Space Complexity
Selection Sort Space Complexity
O(1) Space Complexity
Quick Sort Space Complexity
O(lg(n)) Space Complexity
Merge Sort Space Complexity
O(n) for arrays. O(lg(n)) for Linked Lists. (Space Complexity)
Merge Sort Computational Complexity
O(n*lg(n))
Heap Sort Computational Complexity
O(n*log(n)) Computational Complexity
Selection Sort Computational Complexity
O(n^2) comparisons, O(n) swaps
Bubble Sort Computational Complexity
O(n^2) swaps and comparisons
Insertion Sort Computational Complexity
O(n^2) swaps and comparisons (Computational Complexity)
Quick Sort Computational Complexity
O(n^2), but typically O(n*lg(n))
Quick Sort Algorithm
Pick a pivot element. Rearrange the array so that all elements smaller than the pivot come before it, and all elements larger come after. Recursively go through each half (the half before the pivot, and the half after), and repeat.
Merge Sort Algorithm
Recursively cut the array in half, until it is a single element (which is implicitly sorted). As you come back up, merge each half together. Merge them by starting with indices at the leftmost element of each half. Put the lowest one into the new merged array first, and increment that index. The result will be a sorted merged array.
Unstable Sorting Algorithms
Selection Sort, Heap Sort, Quicksort
Non-Adaptive Sorting Algorithms
Selection Sort, Merge Sort, Heap Sort, Quick Sort
When to use Heap Sort
Slightly slower than quicksort, but has better worst case time. Less space complexity than merge sort.
Heap Sort Algorithm
Build a max heap from the list (takes O(n) operations). Remove the largest element from the heap, and put it into the array (takes O(lg(n)) operations). Do this n times to construct the sorted array.
Selection Sort Algorithm
Find the smallest element of the array and place it at position 0. Find the smallest element of the array from 1 - end, place it at position 1. Continue..
Adaptive Sorting Algorithms
Insertion Sort, Bubble Sort
Stable Sorting Algorithms
Insertion Sort, Bubble Sort, Merge Sort
Bubble Sort Algorithm
Iterate through the array, comparing every adjacent pair of elements. Swap the elements if they are unsorted. Continue iterating through the array until it is fully sorted.
Insertion Sort Algorithm
Like sorting a bridge hand. Start with the left-most card. At each card, move it left until it is higher than the card to its left.
When to use Bubble Sort
Never.
When to use Insertion Sort
When arrays are small, or nearly sorted.
When to use Merge Sort
When stability is important. Or when using a linked list. Or when random access is much more expensive than sequential access (Like external sorting on tape). Good for parallelization.
When to use Selection Sort
When swaps are very expensive, but comparisons are cheap. Selection sort is linear in swaps.
When to use Quick Sort
When you want the fastest average-case sort. When you want the data sorted in-place, better space complexity than merge sort.