C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
radix_sort.c File Reference

LSD radix sort for non-negative ints, byte-by-byte. More...

#include "sorts.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define RADIX_BUCKETS   256
 

Functions

static void counting_pass (uint32_t *src, uint32_t *dst, size_t n, int byte_idx)
 
void radix_sort_ints (int *arr, size_t n)
 

Detailed Description

LSD radix sort for non-negative ints, byte-by-byte.

Uses 4 passes (one per byte of a 32-bit int) of stable counting sort. Linear in n × bytes — i.e. effectively O(n) when the value range is bounded. Operates on unsigned int internally to keep the byte extraction unambiguous; assumes input is non-negative.

Returns silently without sorting if any value is negative — this is a teaching implementation, not a general-purpose replacement for quicksort.

Definition in file radix_sort.c.

Macro Definition Documentation

◆ RADIX_BUCKETS

#define RADIX_BUCKETS   256

Definition at line 19 of file radix_sort.c.

Function Documentation

◆ counting_pass()

static void counting_pass ( uint32_t *  src,
uint32_t *  dst,
size_t  n,
int  byte_idx 
)
static

Definition at line 21 of file radix_sort.c.

References RADIX_BUCKETS.

Referenced by radix_sort_ints().

◆ radix_sort_ints()

void radix_sort_ints ( int *  arr,
size_t  n 
)

Definition at line 38 of file radix_sort.c.

References counting_pass().