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.
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.
Please write a python code which implements the counting sort algorithm by creating a CountingSort function...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function with an array input in the form of a list. Then write code to sort 3 randomly generated arrays and the data array listed below, print the output clearly for all four test arrays, develop and comment on the growth function of your code. Comment on the Big O notation of the code. ( Please do not forget to comment on your code to...
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT