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...
develop a  multithreaded version of radix sort c language
develop a  multithreaded version of radix sort c language
Write code for A counting sort is a technique to sort an array of n positive...
Write code for A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the...
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.
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....
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be...
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes. Define the efficiency of the following code fragment:             K := 1             repeat                     J := 1                         repeat                              ……..                              J := J * 2                         until J >= N                         K := K + 1                until K >= N
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT