Algorithms · UGC NET: Sorting and Searching Extras
Linear Time Sorting
Overview
Linear-time sorting studies sorting algorithms that can run in time under restricted conditions on the keys.
The main examples are Counting Sort, Radix Sort, and Bucket Sort. They are also called non-comparison sorts or distribution sorts because they use key values, digits, or assumed distribution rather than only pairwise comparisons.
This topic matters because it explains when the usual comparison lower bound can be bypassed, and it appears in UGC NET as direct complexity, stability, trace, and assertion-reason questions.
Before studying this lesson, revise arrays, asymptotic notation, stable sorting, insertion sort, and the comparison lower bound.
Key concepts
- Stable sort: equal keys keep their original relative order. If two records have the same key, the one appearing first in the input must appear first in the output.
- Comparison-based sorting: the algorithm learns order only by comparing pairs of keys. Any such algorithm needs comparisons in the worst case.
- Counting Sort works for integer keys in a small range to . It counts occurrences and uses prefix sums to place each element directly.
- Counting Sort time: ; space: . It is linear only when .
- Radix Sort sorts keys digit by digit. In Least-Significant-Digit (LSD) Radix Sort, it processes digits from least significant to most significant using a stable digit sort.
- LSD Radix Sort needs a stable digit sort. If the digit sort is not stable, earlier passes are destroyed.
- Most-Significant-Digit (MSD) Radix Sort first partitions by the most significant digit, then recursively sorts each partition using remaining lower digits.
- Bucket Sort assumes keys are uniformly distributed over a range such as . It distributes keys into buckets, sorts each bucket, and concatenates buckets.
- Bucket Sort expected time: under uniform distribution with buckets; worst case: if all keys fall into one bucket.
- Linear sorts do not violate the lower bound: the bound is only for comparison-based sorting. Counting, radix, and bucket sort use key structure, indices, digits, or distribution.
- Counting Sort and Radix Sort are not in-place. Bucket Sort is also not in-place.
- Convention used here: counting-sort trace follows CLRS 1-indexed arrays. stores prefix counts of elements . For 0-indexed arrays, subtract one from the placement position.
How it works
1. Counting Sort
- Given with integer keys in , create count array initialised to 0.
- Count frequencies: for each , increment .
- Compute prefix sums: for to , set . Now is the number of elements .
- Place elements from right to left: for down to , set and decrement .
- Copy back to .
Right-to-left placement is what makes standard Counting Sort stable. If you scan left-to-right with decrement placement, equal keys can be reversed. If you use a different convention, such as starting positions and left-to-right increment, stability can also be achieved, but you must be consistent.
2. Radix Sort
For LSD Radix Sort:
- Represent every key in base using digits, with leading zeros.
- For digit position from least significant to most significant: 3. Stable-sort all keys by that digit. Counting Sort is usually used.
- After passes, the keys are sorted.
Correctness follows by induction: after pass , keys are sorted by their lowest digits. A stable next pass preserves this order inside equal next digits.
For MSD Radix Sort:
- Partition keys by the most significant digit.
- Recursively sort each bucket by the next lower digit.
- Stop when a bucket has size 0 or 1, or when digits are exhausted.
LSD is stable if every digit pass is stable. MSD is often implemented recursively and is not automatically stable unless carefully designed.
3. Bucket Sort
- Assume keys are uniform in .
- Create buckets indexed to .
- Put key into bucket .
- Sort each bucket individually, usually by insertion sort.
- Concatenate buckets in increasing bucket order.
The expected bucket size is . If all keys fall into one bucket, insertion sort costs .
4. Why the lower bound is not violated
Comparison sorting can be modelled as a binary decision tree. There are possible permutations of distinct keys, so some leaf needs depth at least . Counting Sort avoids comparisons by using key values as array indices.
Radix Sort avoids them by processing digits. Bucket Sort avoids them by assuming a distribution. These methods work only under extra assumptions or with extra space.
Drawing the diagram…
Formulas
-
Let be the number of keys, the maximum key value for Counting Sort, the radix base, the number of digits, and an exclusive upper bound so keys lie in .
-
Counting Sort: and space .
-
Counting Sort is linear when .
-
Radix Sort number of digits: .
-
LSD Radix Sort with Counting Sort per digit: .
-
If each digit has range to , Radix Sort costs .
-
Bucket Sort expected elements per bucket: .
-
Bucket Sort expected time under uniform distribution: ; worst case: .
-
Comparison sorting lower bound: worst-case comparisons.
-
Decision-tree height for distinct keys: at least .
Worked examples
Example 1: Counting Sort trace.
Let , , keys in .
Counts: , , , .
Prefix sums: .
| Step from right | Old | Place at | New | |
|---|---|---|---|---|
| 2 | 4 | 3 | ||
| 3 | 6 | 5 | ||
| 0 | 1 | 0 | ||
| 1 | 3 | 2 | ||
| 3 | 5 | 4 | ||
| 1 | 2 | 1 |
Output: . Equal 1s and equal 3s keep input order, so the sort is stable.
Example 2: LSD Radix Sort trace.
Take in base 10, treating as .
Pass on units digit: has 1, has 3, has 2, has 3. Stable sort gives .
Pass on tens digit: tens of are . Stable sort gives , which is sorted.
Example 3: Radix Sort complexity two-step.
Suppose integers lie in to , and base .
Here . Number of digits .
Each pass costs .
Total cost .
If instead , then , each pass costs , and total cost is . Base choice changes the answer.
Example 4: Bucket Sort expected cost.
For uniform keys in and 100 buckets, expected keys per bucket is 1. The expected sum of squared bucket sizes is about .
Insertion sort inside buckets does work proportional to these squared sizes, so expected total work is . If all 100 keys land in one bucket, cost becomes , i.e. worst-case .
Example 5: Statement type.
Statement I: Counting Sort is stable if placement scans input right-to-left after standard prefix sums. True.
Statement II: LSD Radix Sort requires a stable digit-sorting subroutine. True.
Statement III: Bucket Sort has worst-case time for arbitrary input. False; worst case is .
Correct conclusion: I and II only.
Comparison
| Algorithm | Main assumption | Typical time | Extra space | Stability | Worst case |
|---|---|---|---|---|---|
| Counting Sort | integer keys in | stable with right-to-left CLRS placement | |||
| LSD Radix Sort | keys as digits in a base; stable digit sort | stable if every pass is stable | same with stable Counting Sort | ||
| Bucket Sort | uniform distribution over | expected | buckets plus output | can be stable if insertion sort and concatenation preserve order | |
| Comparison sort | only pairwise comparisons | lower bound | varies | varies |
| Confusion | Correct idea |
|---|---|
| Counting Sort is always | It is ; linear only when |
| LSD Radix can use any digit sort | It must use a stable digit sort |
| MSD Radix processes least significant digit first | MSD processes most significant digit first, then recurses |
| Bucket Sort worst case is linear | Worst case is ; linear is expected under uniform distribution |
| Linear sorts disprove the lower bound | The lower bound applies only to comparison-based sorting |
Revision summary
- Counting Sort = count, prefix sum, place right-to-left, copy back.
- Standard Counting Sort is stable because the placement pass goes from right to left.
- Counting Sort cost is ; huge makes it impractical.
- LSD Radix Sort needs a stable single-digit sort after every pass.
- Radix cost is passes .
- Number of digits is for keys in .
- MSD Radix sorts by most significant digit first and recurses on buckets.
- Bucket Sort expected only for uniform input; worst .
- Linear sorting does not violate because it is non-comparison-based.
- Counting and radix sort usually use extra arrays; they are not in-place.
- Stability = equal keys keep original relative order.
- Check whether the question uses 1-indexed or 0-indexed placement; off-by-one errors are common.
Previous-year exam insights
- Direct questions ask which algorithm sorts integers in a range such as to in time.
Check whether the chosen method needs only a constant number of digit passes. - Radix-sort complexity questions often test the exact formula.
The correct per-pass cost is , not ; multiply by the number of digits . - Numeric Counting Sort questions test count array, prefix sums, and final placement.
Trace from right to left for CLRS stability. - Statement questions mix up stability, worst case, and assumptions. “Stable”, “uniform”, “comparison-based”, and “worst-case” are the words to check first.
- Match-the-list questions pair algorithms with conditions: Counting Sort with , Bucket Sort with uniform distribution, LSD Radix with passes, and comparison sorting with .
- Assertion-reason questions often say Counting Sort cannot directly sort arbitrary 64-bit integers in because the key range is too large for an auxiliary count array.
- Bucket Sort questions test expected bucket size and expected total cost under uniformity, plus the all-in-one-bucket worst case.
- Code-based questions test the placement loop.
Verify indexing, scan direction, decrement position, and whether stability is preserved.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Define stable sort and explain why LSD Radix Sort needs a stable digit sort.
- Trace Counting Sort on a small array and show count array, prefix sums, output array, and stability.
- Derive the running time of Counting Sort and state exactly when it becomes linear.
- Derive Radix Sort complexity from , , and , and choose a suitable base.
- Trace LSD Radix Sort on two-digit numbers and state the result after each pass.
- Explain the MSD Radix Sort procedure and why it recurses on buckets.
- Explain Bucket Sort assumptions, expected bucket size, expected time, and worst-case time.
- Explain why non-comparison sorts do not violate the lower bound.
- Match Counting Sort, Radix Sort, Bucket Sort, and comparison sorting with their complexities and assumptions.
- Spot off-by-one and stability traps in counting-sort pseudocode.
Attempt the PARIKSHA sub-topic test first, then the subject test.