0% found this document useful (0 votes)
13 views2 pages

Radix Sort

This C program implements radix sort to sort an array of integers. It includes header files for input/output and defines constants. The radix_sort function sorts the array using multiple passes based on the digits of the numbers, placing them into buckets based on their digit values. It is tested on an input array in main.

Uploaded by

rohith16725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Radix Sort

This C program implements radix sort to sort an array of integers. It includes header files for input/output and defines constants. The radix_sort function sorts the array using multiple passes based on the digits of the numbers, placing them into buckets based on their digit values. It is tested on an input array in main.

Uploaded by

rohith16725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<conio.h>
#define MAX 100
#define SHOWPASS
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[MAX], 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;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
}
}
int main() {
int arr[MAX];
int i, num;
clrscr();
printf("\nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("\nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("\nARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\n\nSORTED : ");
print(&arr[0], num);
getch();
return 0;
}

You might also like