What is the fastest way to sort 1 million integers when integers are from the range 1,100?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you need to sort one million integers but every value is known to be between 1 and 100, comparison-based sorting is no longer the best tool. The fastest practical solution is usually counting sort, because the value range is tiny compared with the number of items.
Why Counting Sort Fits This Problem
General-purpose sorts such as quicksort, mergesort, or heapsort spend time comparing elements. That is necessary when values can be arbitrary, but here the domain is fixed and very small. Instead of asking "which element is smaller," you can simply count how many times each value appears.
For numbers from 1 through 100, the count array needs only 101 slots if you ignore index 0.
The algorithm is:
- create an array of counters
- scan the million integers once and increment the appropriate counter
- rebuild the sorted result by writing each number according to its count
That runs in O(n + k) time, where n is the number of values and k is the range size. Here k is only 100, so the work is effectively linear in the size of the input.
A Simple Python Implementation
Output:
This is already very fast. For one million integers in a range of size 100, the counting pass dominates and is extremely cache-friendly.
In-Place Overwrite Is Even Better
If you do not need to preserve the original array separately, you can overwrite it in place after counting.
This avoids allocating a second large result list.
Why General Sorting Loses Here
A comparison sort has to do more work than necessary because it keeps asking relative-order questions for values that come from a tiny known set. Even an O(n log n) sort with a very good implementation still wastes effort compared with a direct frequency count in this scenario.
That does not mean comparison sorts are bad. It means the data distribution gives you a stronger option.
Histograms and Sorting Are Almost the Same Here
For a range this small, the "sorted form" is essentially just a histogram expanded back into a list. If your downstream task can work directly from counts, you may not even need to rebuild the full sorted array.
For example, if the next step only needs:
- frequency of each value
- median
- percentile cutoffs
then the count array itself may be enough, which is even cheaper than materializing the sorted output.
Common Pitfalls
The biggest pitfall is reaching automatically for a library sort without using the range information. With unrestricted inputs that is reasonable, but with values limited to 1 through 100, you are leaving performance on the table.
Another common mistake is forgetting to validate the input range. Counting sort assumes values stay within the expected bounds; one out-of-range value can produce an index error or corrupt the counts.
Developers also sometimes rebuild the full output when all they really needed was the frequency table. If a histogram answers the business question, stop there.
Summary
- For one million integers restricted to
1through100, counting sort is usually the fastest approach. - The algorithm runs in
O(n + k)time, and herekis only100. - A count array plus reconstruction is simpler and faster than general comparison sorting.
- If you only need frequencies or order statistics, the count array may be enough by itself.
- Validate the input range before relying on counting sort logic.

