1. Gyanastra
  2. Algorithms
  3. UGC NET: Sorting and Searching Extras
  4. Linear Time Sorting
Free lesson, no account needed 10 min read 28 practice questions after it
Asked in past papers6 times
  • GATE CSE20081991
  • UGC NET2019201620152013

Algorithms · UGC NET: Sorting and Searching Extras

Linear Time Sorting

Overview

Linear-time sorting studies sorting algorithms that can run in O(n)O(n) 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 Ω(nlog⁡n)\Omega(n \log n) 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 Ω(nlog⁡n)\Omega(n \log n) comparisons in the worst case.
  • Counting Sort works for integer keys in a small range 00 to kk. It counts occurrences and uses prefix sums to place each element directly.
  • Counting Sort time: Θ(n+k)\Theta(n+k); space: Θ(n+k)\Theta(n+k). It is linear only when k=O(n)k=O(n).
  • 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 [0,1)[0,1). It distributes keys into buckets, sorts each bucket, and concatenates buckets.
  • Bucket Sort expected time: Θ(n)\Theta(n) under uniform distribution with nn buckets; worst case: Θ(n2)\Theta(n^2) if all keys fall into one bucket.
  • Linear sorts do not violate the lower bound: the Ω(nlog⁡n)\Omega(n \log n) 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. C[0..k]C[0..k] stores prefix counts of elements ≤i\le i. For 0-indexed arrays, subtract one from the placement position.

How it works

1. Counting Sort

  1. Given A[1..n]A[1..n] with integer keys in 0..k0..k, create count array C[0..k]C[0..k] initialised to 0.
  2. Count frequencies: for each A[j]A[j], increment C[A[j]]C[A[j]].
  3. Compute prefix sums: for i=1i=1 to kk, set C[i]=C[i]+C[i−1]C[i]=C[i]+C[i-1]. Now C[i]C[i] is the number of elements ≤i\le i.
  4. Place elements from right to left: for j=nj=n down to 11, set B[C[A[j]]]=A[j]B[C[A[j]]]=A[j] and decrement C[A[j]]C[A[j]].
  5. Copy BB back to AA.

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:

  1. Represent every key in base bb using dd digits, with leading zeros.
  2. For digit position 11 from least significant to most significant: 3. Stable-sort all nn keys by that digit. Counting Sort is usually used.
  3. After dd passes, the keys are sorted.

Correctness follows by induction: after pass pp, keys are sorted by their lowest pp digits. A stable next pass preserves this order inside equal next digits.

For MSD Radix Sort:

  1. Partition keys by the most significant digit.
  2. Recursively sort each bucket by the next lower digit.
  3. 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

  1. Assume nn keys are uniform in [0,1)[0,1).
  2. Create nn buckets indexed 00 to n−1n-1.
  3. Put key xx into bucket ⌊nx⌋\lfloor n x \rfloor.
  4. Sort each bucket individually, usually by insertion sort.
  5. Concatenate buckets in increasing bucket order.

The expected bucket size is n×(1/n)=1n \times (1/n)=1. If all keys fall into one bucket, insertion sort costs Θ(n2)\Theta(n^2).

4. Why the lower bound is not violated

Comparison sorting can be modelled as a binary decision tree. There are n!n! possible permutations of distinct keys, so some leaf needs depth at least log⁡2(n!)=Ω(nlog⁡n)\log_2(n!)=\Omega(n\log n). 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.

Figure. The prefix sums give final positions, and placing elements from right to left preserves stability, leading to the stable sorted output.

Formulas

  • Let nn be the number of keys, kk the maximum key value for Counting Sort, bb the radix base, dd the number of digits, and UU an exclusive upper bound so keys lie in [0,U−1][0,U-1].

  • Counting Sort: T(n,k)=Θ(n+k)T(n,k)=\Theta(n+k) and space S(n,k)=Θ(n+k)S(n,k)=\Theta(n+k).

  • Counting Sort is linear when k=O(n)k=O(n).

  • Radix Sort number of digits: d=⌈log⁡bU⌉d=\left\lceil \log_b U \right\rceil.

  • LSD Radix Sort with Counting Sort per digit: T(n,b,d)=Θ(d(n+b))T(n,b,d)=\Theta(d(n+b)).

  • If each digit has range 00 to k−1k-1, Radix Sort costs Θ(d(n+k))\Theta(d(n+k)).

  • Bucket Sort expected elements per bucket: n⋅1n=1n \cdot \frac{1}{n}=1.

  • Bucket Sort expected time under uniform distribution: Θ(n)\Theta(n); worst case: Θ(n2)\Theta(n^2).

  • Comparison sorting lower bound: Ω(nlog⁡n)\Omega(n\log n) worst-case comparisons.

  • Decision-tree height for nn distinct keys: at least ⌈log⁡2(n!)⌉\left\lceil \log_2(n!) \right\rceil.

Worked examples

Example 1: Counting Sort trace.
Let A=[1,3,1,0,3,2]A=[1,3,1,0,3,2], n=6n=6, keys in 0..30..3.
Counts: C[0]=1C[0]=1, C[1]=2C[1]=2, C[2]=1C[2]=1, C[3]=2C[3]=2.
Prefix sums: C=[1,3,4,6]C=[1,3,4,6].

Step from rightA[j]A[j]Old C[A[j]]C[A[j]]Place atNew C[A[j]]C[A[j]]
j=6j=624B[4]=2B[4]=23
j=5j=536B[6]=3B[6]=35
j=4j=401B[1]=0B[1]=00
j=3j=313B[3]=1B[3]=12
j=2j=235B[5]=3B[5]=34
j=1j=112B[2]=1B[2]=11

Output: B=[0,1,1,2,3,3]B=[0,1,1,2,3,3]. Equal 1s and equal 3s keep input order, so the sort is stable.

Example 2: LSD Radix Sort trace.
Take [21,13,12,03][21,13,12,03] in base 10, treating 33 as 0303.
Pass on units digit: 2121 has 1, 1313 has 3, 1212 has 2, 0303 has 3. Stable sort gives [21,12,13,03][21,12,13,03].
Pass on tens digit: tens of 21,12,13,0321,12,13,03 are 2,1,1,02,1,1,0. Stable sort gives [03,12,13,21][03,12,13,21], which is sorted.

Example 3: Radix Sort complexity two-step.
Suppose nn integers lie in 00 to n4−1n^4-1, and base b=n2b=n^2.
Here U=n4U=n^4. Number of digits d=⌈log⁡n2(n4)⌉=⌈4/2⌉=2d=\lceil \log_{n^2}(n^4)\rceil=\lceil 4/2\rceil=2.
Each pass costs O(n+b)=O(n+n2)=O(n2)O(n+b)=O(n+n^2)=O(n^2).
Total cost O(d(n+b))=O(2(n+n2))=O(n2)O(d(n+b))=O(2(n+n^2))=O(n^2).
If instead b=nb=n, then d=⌈log⁡n(n4)⌉=4d=\lceil \log_n(n^4)\rceil=4, each pass costs O(n+n)=O(n)O(n+n)=O(n), and total cost is O(4n)=O(n)O(4n)=O(n). Base choice changes the answer.

Example 4: Bucket Sort expected cost.
For n=100n=100 uniform keys in [0,1)[0,1) and 100 buckets, expected keys per bucket is 1. The expected sum of squared bucket sizes is about 2n−1=1992n-1=199.
Insertion sort inside buckets does work proportional to these squared sizes, so expected total work is O(n)O(n). If all 100 keys land in one bucket, cost becomes O(1002)O(100^2), i.e. worst-case O(n2)O(n^2).

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 O(n)O(n) time for arbitrary input. False; worst case is O(n2)O(n^2).
Correct conclusion: I and II only.

Comparison

AlgorithmMain assumptionTypical timeExtra spaceStabilityWorst case
Counting Sortinteger keys in 0..k0..kΘ(n+k)\Theta(n+k)Θ(n+k)\Theta(n+k)stable with right-to-left CLRS placementΘ(n+k)\Theta(n+k)
LSD Radix Sortkeys as dd digits in a base; stable digit sortΘ(d(n+b))\Theta(d(n+b))Θ(n+b)\Theta(n+b)stable if every pass is stablesame with stable Counting Sort
Bucket Sortuniform distribution over [0,1)[0,1)expected Θ(n)\Theta(n)Θ(n)\Theta(n) buckets plus outputcan be stable if insertion sort and concatenation preserve orderΘ(n2)\Theta(n^2)
Comparison sortonly pairwise comparisonsΩ(nlog⁡n)\Omega(n\log n) lower boundvariesvariesΩ(nlog⁡n)\Omega(n\log n)
ConfusionCorrect idea
Counting Sort is always O(n)O(n)It is O(n+k)O(n+k); linear only when k=O(n)k=O(n)
LSD Radix can use any digit sortIt must use a stable digit sort
MSD Radix processes least significant digit firstMSD processes most significant digit first, then recurses
Bucket Sort worst case is linearWorst case is O(n2)O(n^2); linear is expected under uniform distribution
Linear sorts disprove the lower boundThe 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 Θ(n+k)\Theta(n+k); huge kk makes it impractical.
  • LSD Radix Sort needs a stable single-digit sort after every pass.
  • Radix cost is dd passes ×\times O(n+b)=O(d(n+b))O(n+b)=O(d(n+b)).
  • Number of digits is d=⌈log⁡bU⌉d=\lceil \log_b U\rceil for keys in [0,U−1][0,U-1].
  • MSD Radix sorts by most significant digit first and recurses on buckets.
  • Bucket Sort expected Θ(n)\Theta(n) only for uniform input; worst Θ(n2)\Theta(n^2).
  • Linear sorting does not violate Ω(nlog⁡n)\Omega(n\log n) 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 nn integers in a range such as 00 to n2−1n^2-1 in O(n)O(n) 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 O(n+k)O(n+k), not O(nk)O(nk); multiply by the number of digits dd.
  • 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 k=O(n)k=O(n), Bucket Sort with uniform distribution, LSD Radix with dd passes, and comparison sorting with Ω(nlog⁡n)\Omega(n\log n).
  • Assertion-reason questions often say Counting Sort cannot directly sort arbitrary 64-bit integers in O(n)O(n) 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:

  1. Define stable sort and explain why LSD Radix Sort needs a stable digit sort.
  2. Trace Counting Sort on a small array and show count array, prefix sums, output array, and stability.
  3. Derive the running time of Counting Sort and state exactly when it becomes linear.
  4. Derive Radix Sort complexity from dd, bb, and UU, and choose a suitable base.
  5. Trace LSD Radix Sort on two-digit numbers and state the result after each pass.
  6. Explain the MSD Radix Sort procedure and why it recurses on buckets.
  7. Explain Bucket Sort assumptions, expected bucket size, expected time, and worst-case time.
  8. Explain why non-comparison sorts do not violate the Ω(nlog⁡n)\Omega(n\log n) lower bound.
  9. Match Counting Sort, Radix Sort, Bucket Sort, and comparison sorting with their complexities and assumptions.
  10. Spot off-by-one and stability traps in counting-sort pseudocode.

Attempt the PARIKSHA sub-topic test first, then the subject test.

Pages 1–2 of 2