# C Exercises: Radix sort algorithm

## C Programming Searching and Sorting Algorithm: Exercise-8 with Solution

Write a C program to sort a list of elements using the radix sort algorithm.

Note:
Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.

Sample C Code:

```#include  <stdio.h>

void print(int *a, int n) {
int i;
for (i = 0; i < n;  i++)
printf("%d\t", a[i]);
}

void radix_sort(int *a, int n) {
int i, b[10], m = 0,  exp = 1;
for (i = 0; i < n;  i++) {
if (a[i] > m)
m = a[i];
}

while (m / exp >  0) {
int box[10] = { 0 };
for (i = 0; i <  n; i++)
box[a[i] / exp %  10]++;
for (i = 1; i <  10; i++)
box[i] += box[i -  1];
for (i = n - 1; i  >= 0; i--)
b[--box[a[i] / exp  % 10]] = a[i];
for (i = 0; i <  n; i++)
a[i] = b[i];
exp *= 10;
}
}

int main() {
int arr[10];
int i, num;

printf("Input  number of elements: ");
scanf("%d",  &num);

printf("\nInput  array elements one by one : ");
for (i = 0; i <  num; i++)
scanf("%d", &arr[i]);

printf("\nArray  elements : ");
print(&arr[0],  num);

printf("\nSorted  elements : ");
print(&arr[0],  num);

return 0;
}
```

Flowchart :

C Code Editor:

```#include  <stdio.h>

void print(int *a, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}

void radix_sort(int *a, int n) {
int i, b[10], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}

while (m / exp > 0) {
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
}
}

int main() {
int arr[10];
int i, num;

printf("Input number of elements: ");
scanf("%d", &num);

printf("\nInput array elements one by one : ");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

printf("\nArray elements : ");
print(&arr[0], num);

printf("\nSorted elements : ");
print(&arr[0], num);

return 0;
}
```

Improve this sample solution and post your code through Disqus.

﻿