繁体   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