In: Computer Science
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from
the low order to high order. The input array is A = {329, 457, 657, 839, 436, 720, 353}
Radix Sort C++
#include <iostream>
using namespace std;
int Max(int a[],int n)
{
int max=a[0];
for (int i=1;i<n;i++)
if (a[i]>max)
max=a[i];
return max;
}
void countSort(int a[],int n,int e)
{
int op[n];
int i,count[10]={0};
for(i =0;i<n;i++)
count[(a[i]/e)%10]++;
for(i=1;i<10;i++)
count[i]+=count[i-1];
for(i=n-1;i>=0;i--) {
op[count[(a[i]/e)%10]-1]=a[i];
count[(a[i]/e)%10]--;
}
for(i=0;i<n;i++)
a[i]=op[i];
}
void radixSort(int a[],int n)
{
int max=Max(a,n);
for(int e=1;max/e>0;e*=10)
countSort(a,n,e);
}
void print(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<a[i] << "
";
}
int main()
{
int A[] = {329, 457, 657, 839, 436, 720, 353};
int n=sizeof(A)/sizeof(A[0]);
radixSort(A,n);
print(A,n);
return 0;
}
if you like the answer please provide a thumbs up