Question

In: Computer Science

(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the...

(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}

Solutions

Expert Solution

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


Related Solutions

(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18,...
(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18, 5, 7, 16, 10, 9, 3, 12, 14, 0}
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND...
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort for String objects? If yes, explain how. If not, explain why....
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19,...
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if the input range is 0-19.
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
Write a program to display (on 7-segment) continuous up-counting numbers from 00 to 99 (in decimal...
Write a program to display (on 7-segment) continuous up-counting numbers from 00 to 99 (in decimal system) when switch SW2 is pressed and released, and down-counting when switch SW3 is pressed and released. PLEASE CODE IN C. PROVIDE ALGORITHM ALSO.
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4,...
Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4, 3
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT