簡體   English   中英

qsort()不適用於數字數組

[英]qsort() not working for array of number

實際上,我必須為此創建霍夫曼樹,我需要對頻率進行排序,並且為此我使用了qsort()函數。 但是,當我嘗試顯示頻率時,它仍然顯示相同的模式(而不是排序的模式)。 這是我的代碼:

        struct node
        {
            int value;
            char letter;                 /* symbol */
            struct node *left,*right;    /* left and right subtrees */
        };
    typedef struct node Node;
//Given below is the frequency of all 27 alphabets
int englishLetterFrequencies [27] = {81, 15, 28, 43, 128, 23, 20, 61, 71, 2, 1, 40, 24, 69, 76, 20, 1, 61, 64, 91, 28, 10, 24, 1, 20, 1, 130};

這是我嘗試構建huffman(位於main()內)的函數:

/*builds the huffman tree and returns its address by reference*/

    void buildHuffmanTree(Node **tree){
        Node *temp;
        Node *array[27];
        int i, subTrees = 27;
        int smallOne;

        for (i=0;i<27;i++)
        {
            array[i] = malloc(sizeof(Node));
            array[i]->value = englishLetterFrequencies[i];
            array[i]->letter = i;
            array[i]->left = NULL;
            array[i]->right = NULL;
        }
         smallOne=sorting(array); //this function is responsible for sorting. I HAVE QSORT() CALL IN THIS FUNCTION

    return;
}

參見其功能定義:

int sorting(Node *array[])
{
    int smaller;
    int i = 0; int d,p;
    printf("the array frequency is \n");
    for(d=0;d < 27;d++)
    printf("%d  ",*array[d]);
    // sorting of arrays
    qsort(array,27,sizeof(*array),&cmpfunc); 
    //////////////////////////
    printf("\n the sorted array frequency is \n");
        for(p=0;p < 27;p++)
    printf("%d  ",*array[p]);

    return smaller;
}

而cmpfunc()就像這樣//這里可能是錯誤的

 int cmpfunc (const void * a, const void * b)
    {
          return ( ((Node *)a)->value - ((Node *)b)->value );
    }

知道為什么它不對數組排序嗎?

 return ( (*(int**)a) - (*(int**)b ));

這將ab強制轉換為“ pointer-to-pointer-to-int”,因此僅對它們進行一次解引用,然后計算兩個指針之間的差。 您的意思是:

 return ( (*(Node **)a)->value - (*(Node **)b)->value );

因為在這種情況下**(int**)a可能會起作用,但它會使試圖理解代碼的人大為困惑。

編輯:對不起,我最終錯過了自己的取消引用-已修復。

也:

printf("%d  ",*array[d]);

應該

printf("%d  ",array[d]->value);

出於同樣的原因。

您的比較器是錯誤的,並且您對qsort工作原理的假設並不遙遠。

為了對Node*的指針數組進行排序,您需要一個類似的比較器:

int cmpfunc (const void * a, const void * b)
{
    const Node **lhs = (const Node **)a;
    const Node **rhs = (const Node **)b;
    return (*lhs)->value < (*rhs)->value ? -1
        : ((*rhs)->value < (*lhs)->value ? 1 : 0);
}

剝離所有多余的垃圾,包括sorting()調用並直​​接調用qsort ...

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int value;
    char letter;                 /* symbol */
    struct node *left,*right;    /* left and right subtrees */
};
typedef struct node Node;

static const int englishLetterFrequencies [27] =
{
    81, 15, 28, 43, 128, 23,
    20, 61, 71, 2, 1, 40, 24,
    69, 76, 20, 1, 61, 64, 91,
    28, 10, 24, 1, 20, 1, 130
};

int cmpfunc (const void * a, const void * b)
{
    const Node **lhs = (const Node **)a;
    const Node **rhs = (const Node **)b;
    return (*lhs)->value < (*rhs)->value ? -1
        : ((*rhs)->value < (*lhs)->value ? 1 : 0);
}

void buildHuffmanTree(Node **tree)
{
    Node *array[27];
    int i;

    for (i=0;i<27;i++)
    {
        array[i] = malloc(sizeof(*array[i]));
        array[i]->value = englishLetterFrequencies[i];
        array[i]->letter = (char)i;
        array[i]->left = NULL;
        array[i]->right = NULL;
    }

    qsort(array, 27, sizeof(*array), cmpfunc);

    for (i=0; i<27; ++i)
        printf("array[%d]->value = %d\n", i, array[i]->value);

    return;
}

int main()
{
    buildHuffmanTree(NULL);
    return 0;
}

輸出量

array[0]->value = 1
array[1]->value = 1
array[2]->value = 1
array[3]->value = 1
array[4]->value = 2
array[5]->value = 10
array[6]->value = 15
array[7]->value = 20
array[8]->value = 20
array[9]->value = 20
array[10]->value = 23
array[11]->value = 24
array[12]->value = 24
array[13]->value = 28
array[14]->value = 28
array[15]->value = 40
array[16]->value = 43
array[17]->value = 61
array[18]->value = 61
array[19]->value = 64
array[20]->value = 69
array[21]->value = 71
array[22]->value = 76
array[23]->value = 81
array[24]->value = 91
array[25]->value = 128
array[26]->value = 130

花一些時間學習算法的工作原理,不要走捷徑。 您將需要兩個比較器函數來實現最終的功能(假設我了解您打算如何構建霍夫曼樹)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM