AlgoHay
The Journal
Bubble, Selection and Insertion Sort in Python
The three simple sorts are all O(n^2) in the worst case, but they behave very differently on sorted input, in how many swaps they make, and in stability.
Merge Sort and Quicksort: How They Work and What They Cost
Both sorts divide the list in two and recurse. Merge sort does the work when combining and guarantees O(n log n); quicksort does it when splitting and is O(n^2) in the worst case.
Greedy Algorithms vs Dynamic Programming: When Greedy Works
Greedy algorithms are fast and simple, and often wrong. Learn the property that makes a greedy choice safe, see a coin system where greedy fails and dynamic programming does not, and test a greedy idea before trusting it.
Divide and Conquer: Merge Sort, Binary Search and Fast Powers
Divide and conquer splits a problem into smaller copies, solves them and combines the answers. See its three common shapes, the recurrence each produces, and when splitting does not pay.
Hash Tables: Hashing, Collisions and Python dict and set
A hash table turns a key into an array index, which gives O(1) lookup, insert and delete on average. Collisions and resizing explain the conditions on that promise.
Linked Lists in Python: Nodes, Pointers and Their Costs
A linked list stores each item in its own node with a pointer to the next one. It makes insertion at a known node O(1) and gives up O(1) indexing in return.
Heaps and Priority Queues: heapq, Top-k and Heap Sort
A binary heap keeps the smallest item at the front of an array and restores that order in O(log n) after each change. It is the standard way to build a priority queue.
Arrays and Dynamic Arrays: How a Python List Really Works
An array stores items side by side in memory, which makes indexing O(1). A Python list is a dynamic array: append is amortized O(1), but inserting at the front shifts every item.
Stacks and Queues in Python: LIFO, FIFO and deque
A stack hands back the most recent item first and a queue the oldest. In Python a list makes a good stack, and collections.deque makes a good queue.
Sorting Algorithms Compared: Time, Space and Stability
Eight sorting algorithms side by side, with the assumption behind every bound, plus the comparison-sort lower bound, what stability means, and how to choose.