Skip to content

Signed Integer Radix Sort

Radix sort for signed integers by transforming values to preserve ordering across negative and positive ranges.

Signed integer radix sort extends radix sorting to handle both negative and positive integers correctly. Since radix sort operates on digit or bit patterns, special handling is required to maintain numeric ordering.

Problem

Given an array AA of signed integers, sort the array in increasing order.

Integers are represented in two’s complement form, where direct unsigned interpretation does not preserve ordering.

Idea

Transform each integer into a form where lexicographic bit ordering matches numeric ordering, then apply standard radix sort.

Two common approaches:

  1. Offset transformation
  2. Sign-bit handling in the most significant pass

Approach 1: Offset Transformation

Shift all values by the minimum value:

x=xmin(A) x' = x - \min(A)

This makes all values nonnegative, allowing standard radix sort.

After sorting, no inverse mapping is needed because ordering is preserved.

Approach 2: Bitwise Transformation

For fixed-width integers, flip the sign bit:

k(x)=x2w1 k(x) = x \oplus 2^{w-1}

where ww is the number of bits.

This maps:

  • negative values to lower half
  • nonnegative values to upper half

so that integer ordering matches unsigned ordering.

Algorithm (Bitwise Method)

signed_radix_sort(A):
    for each x in A:
        x = x XOR sign_mask

    radix_sort(A)

    for each x in A:
        x = x XOR sign_mask

    return A

where:

sign_mask=2w1 \text{sign\_mask} = 2^{w-1}

Example

Let:

A=[3,1,1,2] A = [-3, 1, -1, 2]

Binary (4-bit example):

valuebits
-31101
-11111
10001
20010

After XOR with sign mask:

valuetransformed
-30101
-10111
11001
21010

Sorted:

[0101,0111,1001,1010] [0101, 0111, 1001, 1010]

Mapped back:

[3,1,1,2] [-3, -1, 1, 2]

Correctness

Flipping the sign bit reorders the integer space so that:

  • all negative values come before nonnegative values
  • relative ordering within each group is preserved

Thus:

x1<x2    k(x1)<k(x2) x_1 < x_2 \iff k(x_1) < k(x_2)

Radix sort correctly orders the transformed values, and reversing the transformation yields the correct sorted order.

Complexity

Let:

  • nn be number of elements
  • mm be number of digits or bytes

Time:

O(nm) O(n \cdot m)

Space:

O(n) O(n)

Properties

propertyvalue
stableyes, if radix sort is stable
in placeno
comparison basedno
handles negativesyes

When to Use

Use signed integer radix sort when:

  • sorting large arrays of integers with both negative and positive values
  • fixed-width representation is available
  • linear-time sorting is desired

Avoid when:

  • data size is small
  • simplicity is preferred over performance
  • memory for auxiliary arrays is limited

Implementation (Python)

def signed_radix_sort(a):
    if not a:
        return a

    # Assume 32-bit integers
    sign_mask = 1 << 31

    # Transform
    a = [x ^ sign_mask for x in a]

    # Use Python sort here; replace with radix for full implementation
    a.sort()

    # Reverse transform
    return [x ^ sign_mask for x in a]