Radix Sort Implementation
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++.
Interview preparation material.
165 notes tagged
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++.
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++.
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.
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++.
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.
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...
The "binary search on the answer" pattern explained: when the answer space has a monotone yes/no predicate, binary-search the answer itself. With a re...
Lower bound vs upper bound binary search explained: first index >= target vs first index > target, with ready templates in Python, Java and C++.
Random Pick with Weight (LeetCode 528): build prefix sums and binary-search a random target for O(log n) weighted sampling.
Find Right Interval (LeetCode 436): sort interval starts and binary-search the smallest start >= each interval end, O(n log n).