Radix Sort zaimplementowany w C ++

Próbuję ulepszyć moje C ++, tworząc program, który zajmie dużą liczbę liczb od 1 do 10 ^ 6. Segmenty, w których będą przechowywane liczby w każdym przejściu, to tablica węzłów (gdzie węzeł jest utworzoną strukturą I zawierającą wartość i następny atrybut węzła).

Po posortowaniu liczb do segmentów według najmniejszej znaczącej wartości, kończę jeden punkt wiadra na początku innego wiadra (dzięki czemu mogę szybko zapisać liczby bez zakłócania kolejności). Mój kod nie zawiera błędów (kompilacja lub środowisko wykonawcze), ale trafiłem na ścianę, aby dowiedzieć się, jak rozwiązać pozostałe 6 iteracji (ponieważ znam zakres liczb).

Mam problem z tym, że początkowo liczby były dostarczane do funkcji radixSort w postaci tablicy int. Po pierwszej iteracji sortowania liczby są teraz przechowywane w tablicy struktur. Czy jest jakiś sposób, w jaki mógłbym przerobić mój kod tak, żebym miał tylko jedną pętlę for dla 7 iteracji, czy też potrzebuję jednej pętli, która zostanie uruchomiona jeden raz, a kolejna pętla poniżej, która będzie działać 6 razy, zanim zwróci całkowicie posortowaną lista?

#include <iostream>
#include <math.h>
using namespace std;

struct node
{
    int value;
    node *next; 
};

//The 10 buckets to store the intermediary results of every sort
node *bucket[10];
//This serves as the array of pointers to the front of every linked list
node *ptr[10];
//This serves as the array of pointer to the end of every linked list
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;

    //Perform radix sort
    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); 
        }//End of insertion loop
        k=0,l=1;

        //Linking loop: Link end of one linked list to the front of another
        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++;   
            }

        }//End of linking for loop

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

        for(j=0;j<10;j++)
            bucket[i]->next=NULL;                       
        cout << "End of iteration" <<endl;
    }//End of radix sort loop
}

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

questionAnswers(3)

yourAnswerToTheQuestion