8

1 から 10^6 までの数を大量に取るプログラムを作成して、C++ を改善しようとしています。各パスで数値を格納するバケットは、ノードの配列です (ノードは、値と次のノード属性を含む作成した構造体です)。

最下位の値に従って数値をバケットに並べ替えた後、1 つのバケットの末尾が別のバケットの先頭を指すようにします (順序を乱すことなく格納されている数値をすばやく取得できるようにするため)。私のコードにはエラー (コンパイルまたは実行時) はありませんが、残りの 6 回の繰り返しを解決する方法に関して壁にぶつかりました (数値の範囲を知っているため)。

私が抱えている問題は、最初に数値が int 配列の形式で radixSort 関数に提供されたことです。並べ替えの最初の繰り返しの後、数値は構造体の配列に格納されます。コードを作り直して、7回の反復でforループを1つだけにする方法はありますか?または、1回実行されるforループが1つ必要で、その下に6回実行されてから完全にソートされたものを返す別のループが必要ですか?リスト?

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

3 に答える 3

14

私はあなたがあなたの解決策をひどく過度に複雑にしていると思います。入力で受け取った単一の配列を使用して基数を実装できます。各ステップのバケットは、入力配列の各バケットの開始インデックスをマークするインデックスの配列で表されます。

実際、再帰的に行うこともできます。

// Sort 'size' number of integers starting at 'input' according to the 'digit'th digit
// For the parameter 'digit', 0 denotes the least significant digit and increases as significance does
void radixSort(int* input, int size, int digit)
{
    if (size == 0)
        return;

    int[10] buckets;    // assuming decimal numbers

    // Sort the array in place while keeping track of bucket starting indices.
    // If bucket[i] is meant to be empty (no numbers with i at the specified digit),
    // then let bucket[i+1] = bucket[i]

    for (int i = 0; i < 10; ++i)
    {
        radixSort(input + buckets[i], buckets[i+1] - buckets[i], digit+1);
    }
}

もちろん、が9のbuckets[i+1] - buckets[i]場合、バッファオーバーフローが発生しますがi、余分なチェックや読みやすさのために省略しました。私はあなたがそれを処理する方法を知っていると信じています。

それで、あなたはただ呼び出すradixSort(testcases, sizeof(testcases) / sizeof(testcases[0]), 0)必要があり、あなたの配列はソートされるべきです。

于 2009-08-13T12:12:18.537 に答える