Insertion Sort Implementation
Insertion sort explained: build a sorted prefix by inserting each element into place. O(n^2) worst but O(n) on nearly sorted data; stable, in Python...
5 notes tagged
Insertion sort explained: build a sorted prefix by inserting each element into place. O(n^2) worst but O(n) on nearly sorted data; stable, in Python...
Selection sort explained: repeatedly pick the smallest remaining element and place it next. O(n^2) time with the fewest swaps, in Python, Java and C++...
Bubble sort explained simply: repeatedly swap adjacent out-of-order items, with an early-exit optimisation. O(n^2) time, stable, in Python, Java and C...
Recursive binary search on a sorted array in O(log n) time with O(log n) call-stack space, in Python, Java and C++.
Classic iterative binary search on a sorted array in O(log n) time, with Python, Java and C++ code and an overflow-safe midpoint.