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

Radix Sort

This document contains the code for performing radix sort on an array of integers. The radix sort algorithm sorts data based on the individual digits by making multiple passes over the data. It uses counting sort as a subroutine to sort elements based on their digits. The code first finds the maximum element in the array to determine the number of passes. It then performs counting sort repeatedly by considering each digit of every element from least to most significant digit to sort the array.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Radix Sort

This document contains the code for performing radix sort on an array of integers. The radix sort algorithm sorts data based on the individual digits by making multiple passes over the data. It uses counting sort as a subroutine to sort elements based on their digits. The code first finds the maximum element in the array to determine the number of passes. It then performs counting sort repeatedly by considering each digit of every element from least to most significant digit to sort the array.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* RADIX SORT – COMPLETE CODE */

/* AMITY MCA(II) */

#include<stdio.h>

#define MAX 5
#define SHOWPASS

void print(int *a,int n)


{
int i;
for(i=0;i
printf("%d\t",a[i]);
}

/* BY HEART / LEARN/ DO ONLY BLUE COLOR FUNCTION CODE IF FACING


INTERESTED */
void radixsort(int *a,int n)
{
int i,b[MAX],m=0,exp=1;
int bucket[10]={0};
for(i=0;i
{
if(a[i]>m)
m=a[i];
}

while(m/exp>0)
{

for(i=0;i
bucket[a[i]/exp%10]++;
for(i=1;i<10;i++)
bucket[i]+=bucket[i-1];
for(i=n-1;i>=0;i--)
b[--bucket[a[i]/exp%10]]=a[i];
for(i=0;i
a[i]=b[i];
exp*=10;

#ifdef SHOWPASS
printf("\nPASS : ");
print(a,n);
#endif
}
}

int main()
{
int arr[MAX];
int i,n;

printf("Enter total elements (n < %d) : ",MAX);


scanf("%d",&n);

printf("Enter %d Elements : ",n);


for(i=0;i
scanf("%d",&arr[i]);

printf("\nARRAY : ");
print(&arr[0],n);

radixsort(&arr[0],n);

printf("\nSORTED : ");
print(&arr[0],n);
printf("\n");

return 0;
}

You might also like