Sort n numbers between 0,n2 - 1 in On?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Sorting has always been one of the essential operations in computer science, with a plethora of algorithms ranging from the classic QuickSort to the more sophisticated MergeSort. One intriguing problem is sorting `n` numbers that lie in the range `[0, n^2 - 1]` in linear time, which can be achieved using a variant of Counting Sort.
Theoretical Background
Understanding the Problem
The problem at hand is to sort an array of `n` integers where each integer is located within the range `[0, n^2 - 1]`. The standard comparison-based sorting algorithms like QuickSort, MergeSort, or HeapSort have an average time complexity of . However, given this specific scenario, it is possible to achieve the sorting in time utilizing non-comparison-based sorting.
Why Linear Time Sorting is Possible
The key reason we can achieve sorting is due to the restriction on the range `[0, n^2 - 1]`. Traditional comparison-based algorithms rely on comparing elements, but by utilizing positional or counting information, such algorithms can perform better for constrained data scenarios. In this context, Counting Sort is adapted but with additional trickery that aligns with the limited range.
Sorting Approach
Core Idea: Radix Sort with Counting Sort
The proposed solution involves a combination of the Counting Sort and Radix Sort algorithm. Given the nature of the input, we can decompose the numbers into two digits based on their range, each being less than `n`.
Steps to Sort:
- Digit Extraction: Each number in the range `[0, n^2 - 1]` can be viewed as a two-digit number in base `n`:
- Here, consider `(q, r)` as two parts of a number where:
- `r` = number % `n`
- `q` = number // `n`
- Apply Counting Sort:
- First, apply Counting Sort on the lower significant digit - `r`.
- Next, sort the original array on the upper significant digit - `q` using Counting Sort again.
- Complexity Analysis:
- Since both digits `q` and `r` are smaller than `n`, sorting them individually uses operations each.
- The overall complexity remains as we perform a constant number of operations per element.
Example Code
Here's a simplistic representation of the algorithm:
Related reading
- Sort objects in ArrayList by date?
- Sort on a string that may contain a number
- Sort (order) data frame rows by multiple columns
- sort outer array based on values in inner array, javascript
- Sort points in clockwise order?
- Sort polygon's points for drawing
- Sort the rows according to the order specified in WHERE IN clause
- Sort when only equality is available

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.