Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Implement this algorithm exJ rray the the ing ys lay the the 10 A radix sor

ID: 3829461 • Letter: C

Question

C++: Implement this algorithm

exJ rray the the ing ys lay the the 10 A radix sort is a technique for sorting unsigned integers (or other data that has individual characters or digits) One version of radix sort works with a linked list of unsigned integers. In addition to the list that is be- ing sorted, there are two extra lists called listo and list 1 The algorithm begins by going through the list of elements to be sorted; every even number is trans ferred from the original lis to the tail of list0, every odd number is transferred to the tail of list1 (If you are using the STL list class, you may re- move an item from the head of a list with the pop-front member function, and you may add a new node to the tail of a list with push back.) After all numbers have been processed, put the two lists together (with list0 in front and list1 at the back), and then put the whole thing back in the original list. With the STL list class, this can be done in constant time with two splice statements shown here: splice (original begin list1); splice original begin list0) In this code, original is the original list (that is empty before the two splices because we moved ev- erything to listo and list1) The process of separating and splicing is repeat- ed a second time, but now we separate based on the boolean expression (Cn/2) 2 0). And then we separate and splice using (Cn/4) 2 And so on with larger and larger divisors 8, 16, 32, The process stops when the divisor is bigger than the largest number in the list. Here is one way to implement the algorithm:

Explanation / Answer

#include<iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[10] = {0};
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = {150,30,190,250,70};
int n = sizeof(arr)/sizeof(arr[0]);
radixsort(arr, n);
print(arr, n);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote