Table of contents
1.
Introduction
2.
Radix Sort Algorithm
3.
Example
3.1.
C
4.
Time & Space Complexity
4.1.
Time Complexity
4.2.
Space Complexity
5.
Frequently Asked Questions
5.1.
Is radix sort stable?
5.2.
Can radix sort handle negative numbers?
5.3.
Is radix sort an in-place sorting algorithm?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Radix Sort in C

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Sorting is a basic task in computer science. There are many ways to sort a list of numbers or other data. Radix sort is one easy & fast way to sort integers. 

Radix Sort in C

In this article, we will learn what radix sort is, how it works with examples, and its time & space complexity. We will also see how to code radix sort in the C programming language from scratch.

Radix Sort Algorithm

Radix sort works by sorting the input numbers one digit at a time, from least to most significant digit. It is a non-comparative sorting algorithm, meaning it doesn't compare elements to each other to decide the sorting order.

The algorithm works with these steps:

  1. Find the largest number in the input array to determine the number of digits.
     
  2. Starting from the rightmost (least significant) digit, sort all numbers based on that digit using a stable sorting algorithm like counting sort. Numbers are grouped into buckets based on the current digit.
     
  3. Repeat step 2 for the next digit to the left, until all digits including the leftmost (most significant) digit have been considered.

Example

  • C

C

#include <stdio.h>

// Helper function to get the maximum value from the array

int getMax(int arr[], int n) {

   int max = arr[0];

   for (int i = 1; i < n; i++)

       if (arr[i] > max)

           max = arr[i];

   return max;

}

// Function to perform counting sort on the array according to the digit represented by exp

void countSort(int arr[], int n, int exp) {

   int output[n]; // output array

   int i, count[10] = {0};

   // Store count of occurrences in count[]

   for (i = 0; i < n; i++)

       count[(arr[i] / exp) % 10]++;

   // Change count[i] so that count[i] now contains actual position of this digit in output[]

   for (i = 1; i < 10; i++)

       count[i] += count[i - 1];

   // Build the output array

   for (i = n - 1; i >= 0; i--) {

       output[count[(arr[i] / exp) % 10] - 1] = arr[i];

       count[(arr[i] / exp) % 10]--;

   }

   // Copy the output array to arr[], so that arr[] now contains sorted numbers

   for (i = 0; i < n; i++)

       arr[i] = output[i];

}


// Main function to implement radix sort

void radixSort(int arr[], int n) {

   int m = getMax(arr, n); // Get the maximum number to know the number of digits

   // Do counting sort for every digit. The exp is 10^i where i is the current digit number

   for (int exp = 1; m / exp > 0; exp *= 10)

       countSort(arr, n, exp);

}

// Driver code to test above

int main() {

   int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};

   int n = sizeof(arr) / sizeof(arr[0]);

   radixSort(arr, n);

   printf("Sorted array: ");

   for (int i = 0; i < n; i++)

       printf("%d ", arr[i]);

   printf("\n");

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Sorted array: 2 24 45 66 75 90 170 802 

Time & Space Complexity

Let's analyze the time & space complexity of radix sort.

Time Complexity

  • Let n be the number of elements to sort and d be the number of digits in the largest number.
     
  • In each iteration, radix sort processes all n elements and puts them into buckets based on the current digit. This takes O(n) time.
     
  • There are d iterations, one for each digit. So the total time complexity is O(d * n).
     
  • The value of d depends on the number of digits in the input numbers. If k is the maximum possible value, then d would be O(log k) as the number of digits is the logarithm of the number.
     
  • Therefore, the overall time complexity of radix sort is O((n + k) * log k). It is linear in the number of elements plus the range of input values.

Space Complexity

Radix sort uses extra space for the buckets in each iteration.

The space required is O(n + k), where n is the number of elements and k is the range of input values.

In the worst case, there are n buckets (for example, if all numbers have the same digit in the current place), and each bucket may contain all n elements. So the space complexity is O(n + k).

In summary

  • Time complexity: O((n + k) * log k)
     
  • Space complexity: O(n + k)

Radix sort is fast for integers with a small number of digits. But it can be slower than comparison-based sorting algorithms like quicksort for large numbers of digits.

Frequently Asked Questions

Is radix sort stable?

A: Yes, radix sort is a stable sorting algorithm if the underlying sorting method used for each digit is stable, like counting sort.

Can radix sort handle negative numbers?

A: Radix sort can be modified to handle negative numbers by using the most significant bit as a sign bit and then sorting based on absolute values.

Is radix sort an in-place sorting algorithm?

A: No, radix sort is not an in-place algorithm as it requires extra space for the buckets in each iteration.

Conclusion

In this article, we learned about the radix sort algorithm, which sorts integers by processing individual digits from least to most significant. We saw how the algorithm works with a proper example. We also analyzed the time complexity of O((n + k) * log k) and space complexity of O(n + k) for radix sort. Radix sort is a fast and efficient method for sorting integers when the number of digits is small.

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass