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

C++ Implement this algorithm as shown here exJ rray the the ing ys lay the the 1

ID: 3829463 • Letter: C

Question

C++

Implement this algorithm as shown here

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>
#include <math.h>
using namespace std;
struct node
{
int value;
node *next;
};
node *bucket[10];
node *ptr[10];
node *end[10];
node *linkedpointer;
node *item;
node *temp;
void append(int value, int n)
{
node *temp;
item=new node;
item->value=value;
item->next=NULL;
end[n]=item;
if(bucket[n]->next==NULL)
{
cout << "Bucket " << n << " is empty" <<endl;
bucket[n]->next=item;
ptr[n]=item;
}
else
{
cout << "Bucket " << n << " is not empty" <<endl;
temp=bucket[n];
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=item;
}
}

bool isBucketEmpty(int n){
if(bucket[n]->next!=NULL)
return false;
else
return true;
}
//print the contents of all buckets in order
void printBucket(){
temp=bucket[0]->next;
int i=0;
while(i<10){
if(temp==NULL){
i++;
temp=bucket[i]->next;   
}
else break;

}
linkedpointer=temp;
while(temp!=NULL){
cout << temp->value <<endl;
temp=temp->next;
}
}
void radixSort(int *list, int length){
int i,j,k,l;
int x;
for(i=0;i<10;i++){
bucket[i]=new node;
ptr[i]=new node;
ptr[i]->next=NULL;
end[i]=new node;
}
linkedpointer=new node;
for(i=0;i<1;i++){
for(j=0;j<length;j++){
x=(int)(*(list+j)/pow(10,i))%10;
append(*(list+j),x);
printBucket(x);
}
k=0,l=1;
for(j=0;j<9;j++){
if(isBucketEmpty(k))
k++;
if(isBucketEmpty(l) && l!=9)
l++;
if(!isBucketEmpty(k) && !isBucketEmpty(l)){
end[k]->next=ptr[l];
k++;
if(l!=9) l++;   
}

}
cout << "Print results" <<endl;
printBucket();

for(j=0;j<10;j++)
bucket[i]->next=NULL;   
cout << "End of iteration" <<endl;
}}
int main(){
int testcases,i,input;
cin >> testcases;
int list[testcases];
int *ptr=&list[0];
for(i=0;i<testcases;i++){
cin>>list[i];
}

radixSort(ptr,testcases);
return 0;
}