[go: up one dir, main page]

0% found this document useful (0 votes)
18 views1 page

C Programmers: Radix Sort Example

The document contains a C program that implements the Radix Sort algorithm to sort an array of integers. It includes functions to find the maximum value in the array, perform counting sort based on digit positions, and print the sorted array. The main function initializes an array, reads input values, sorts them using Radix Sort, and prints the sorted result.

Uploaded by

coolscools123
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)
18 views1 page

C Programmers: Radix Sort Example

The document contains a C program that implements the Radix Sort algorithm to sort an array of integers. It includes functions to find the maximum value in the array, perform counting sort based on digit positions, and print the sorted array. The main function initializes an array, reads input values, sorts them using Radix Sort, and prints the sorted result.

Uploaded by

coolscools123
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/ 1

#include<stdio.

h>
int getMax(int nums[],int n)
{
int max=nums[0];
int i;
for(i=1;i<n;i++)
if(nums[i]>max)
max=nums[i];
return max;
}
void CountSort(int nums[],int n,int exp)
{
int output[n];
int i,cnt[10]={0};
for(i=0;i<n;i++)
cnt[(nums[i]/exp)%10]++;
for(i=1;i<10;i++)
cnt[i]+=cnt[i-1];
for(i=n-1;i>=0;i--)
{
output[cnt[(nums[i]/exp)%10]-1]=nums[i];
cnt[(nums[i]/exp)%10]--;
}
for(i=0;i<n;i++)
nums[i]=output[i];
}
void Radixsort(int nums[],int n)
{
int m=getMax(nums,n);
for(int exp=1;m/exp>0;exp*=10)
CountSort(nums,n,exp);
}
void print(int nums[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\n",nums[i]);
}
int main()
{
//printf("enter number of elements:");
int N=6;
//scanf("%d",&N);
int nums[N];
//printf("Enter elements:");
for(int i=0;i<N;i++)
{
scanf("%d",&nums[i]);
}
Radixsort(nums,N);
//printf("sorted array is:");
print(nums,N);
return 0;
}

You might also like