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...
958 published notes across every category.
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...
Merge sort explained: divide the array in half, sort each half, then merge. Guaranteed O(n log n), stable, with Python, Java and C++ code.
Quick sort explained: partition around a pivot and recurse. O(n log n) average, in-place, with the Lomuto partition in Python, Java and C++.
Heap sort explained: build a max-heap, then repeatedly move the max to the end. O(n log n) worst case, in-place, with Python, Java and C++ code.
Counting sort explained: tally occurrences of each value, then rebuild the array. O(n + k) time for small integer ranges, in Python, Java and C++.
Radix sort explained: sort integers digit by digit using a stable counting sort per digit. O(d(n + k)) time, in Python, Java and C++.
Bucket sort explained: scatter elements into buckets by range, sort each bucket, then concatenate. O(n) average for uniform data, in Python, Java and...
Sort an Array (LeetCode 912): how to use built-in sorts and custom comparators in Python, Java and C++, including ascending, descending and key-based...
Merge Intervals (LeetCode 56): sort by start, then merge overlapping intervals in one pass. O(n log n), with Python, Java and C++ code.
Largest Number (LeetCode 179): sort numbers by a custom pairwise rule (a+b vs b+a) to form the biggest concatenation. O(n log n), Python/Java/C++.
Custom Sort String (LeetCode 791): sort one string by the character priority defined in another, using a rank map. O(n log n) or O(n), Python/Java/C++...
Sort Characters By Frequency (LeetCode 451): count characters, then sort by descending frequency and rebuild the string. O(n + k log k), Python/Java/C...