解读 qsort 行为

发布于 2024-12-04 02:46:43 字数 1509 浏览 3 评论 0原文

我需要 qsort 的功能来运行我的程序,但到目前为止还没有完成它的工作。

我本质上是对单个字符值的数组进行排序,以便将它们分组,以便我可以迭代该数组并确定每个属性的计数。我的问题是 qsort 返回一个“已排序”数组,因为

xxxxxbxbbbxfbxfbffffbxfxbbfbbbxxfxbxxfbbbbxbfxbxfbxbsxbbbxxbbxxffxbxfxfxbxxbxxfbbbfbxbbx
bbbsxfxbxbxxbfbfxbxxbxxbfxxbxsbfxxfxfxfffxbfxffbbfffsxsfbfbxbxbbbxxsbfbfbbbbbbxxfxfxffxf
xbxxbxfxbfbxbxxbxbxxbxbbffxxbxxffxxbxfxbxffxfsfxxfxxfxxfxfxxfxxbsxxbbbxsxxbbxxxbxfxsbxxx
ffbxfxxffbxxxfxxfxxfxfxxfffbxxxbxxxfffxsbbfffffxxxbbfxsbffxbxxfxbxxfbbfsbffsfffxfxfxbbffx
bxxfxbxxfxbbbfxxbbfxxbbbsxbxfbfbbxxbbfffxxfxxbbbfxxbxxxbbxxxbfxffxxxffxfxxffbxfsxbxxxfxfx
fsbbbxxxbfxfffsfxxxfssxxxfxfxxxxbxbbbxxbxxxxxxxxxxxxxxxxxxxfbfxxffxxbxxxxxxxsxsxxxxxxxxsxb
bxxxxxfxbxxxxfxxfxxxxxbbxfffbxbsxffbbbxsfbbfffbxbfbbxxbxxbbxxbffxfxxfxfbbxxbxfxxsfxxfxxbxf
xxbxxxbxbxbbxbbffxxxxbfbfxxxxxxfxffxxxxxxxxxxxxxxxxxxxxxbxffxbxbxbbxbbxxfbxfxbxxbxxbxbxxxb
xxbxbxbfbbffffffsbbxxbffbxfxxfxbfbfffsxbxxxsxxbbbbbxxxbxxxfxxfffxxxxxxxxxxxxxfxxbxxxxxxxxx
xxbfbxxxxxxxxxxxxxxxxxxxxxxxxxxbxbxxxxxfxxbxxxxffxbxxxxffxfbfffxbxxfxbfxbxxfxbxbfxxxxxfxbx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsbxxxxffxfxxxxxxxxxfxfxxxbffffxxxfbxbxfxxxxxxxxxxxxxxxxxxxxf
fxfxbfxxxfxxxxx

我认为问题与我的函数调用或比较方法有关。

int compare(const void *a, const void *b){
  return *(char * const *) a - *(char * const *) b;
}

and 用于其中

qsort(temp, lineCount, sizeof(char), compare);

temp 是上面的字符数组,lineCount 是数组中的字符数。阵列的完整性和尺寸均已通过测试验证。

非常感谢任何帮助。

I need the functionality of qsort for my program to run, and so far hasn't been doing its job.

I'm essentially sorting an array of single character values in order to clump them into groups so I can iterate through the array and determine counts for each attribute. My problem is qsort is returning a "sorted" array as

xxxxxbxbbbxfbxfbffffbxfxbbfbbbxxfxbxxfbbbbxbfxbxfbxbsxbbbxxbbxxffxbxfxfxbxxbxxfbbbfbxbbx
bbbsxfxbxbxxbfbfxbxxbxxbfxxbxsbfxxfxfxfffxbfxffbbfffsxsfbfbxbxbbbxxsbfbfbbbbbbxxfxfxffxf
xbxxbxfxbfbxbxxbxbxxbxbbffxxbxxffxxbxfxbxffxfsfxxfxxfxxfxfxxfxxbsxxbbbxsxxbbxxxbxfxsbxxx
ffbxfxxffbxxxfxxfxxfxfxxfffbxxxbxxxfffxsbbfffffxxxbbfxsbffxbxxfxbxxfbbfsbffsfffxfxfxbbffx
bxxfxbxxfxbbbfxxbbfxxbbbsxbxfbfbbxxbbfffxxfxxbbbfxxbxxxbbxxxbfxffxxxffxfxxffbxfsxbxxxfxfx
fsbbbxxxbfxfffsfxxxfssxxxfxfxxxxbxbbbxxbxxxxxxxxxxxxxxxxxxxfbfxxffxxbxxxxxxxsxsxxxxxxxxsxb
bxxxxxfxbxxxxfxxfxxxxxbbxfffbxbsxffbbbxsfbbfffbxbfbbxxbxxbbxxbffxfxxfxfbbxxbxfxxsfxxfxxbxf
xxbxxxbxbxbbxbbffxxxxbfbfxxxxxxfxffxxxxxxxxxxxxxxxxxxxxxbxffxbxbxbbxbbxxfbxfxbxxbxxbxbxxxb
xxbxbxbfbbffffffsbbxxbffbxfxxfxbfbfffsxbxxxsxxbbbbbxxxbxxxfxxfffxxxxxxxxxxxxxfxxbxxxxxxxxx
xxbfbxxxxxxxxxxxxxxxxxxxxxxxxxxbxbxxxxxfxxbxxxxffxbxxxxffxfbfffxbxxfxbfxbxxfxbxbfxxxxxfxbx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsbxxxxffxfxxxxxxxxxfxfxxxbffffxxxfbxbxfxxxxxxxxxxxxxxxxxxxxf
fxfxbfxxxfxxxxx

I think the problem has to do with either my function call or compare method.

int compare(const void *a, const void *b){
  return *(char * const *) a - *(char * const *) b;
}

and is used in

qsort(temp, lineCount, sizeof(char), compare);

where temp is the array of characters above and lineCount is the number of characters in the array. Both the integrity of the array as well as the size have been verified through testing.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-12-11 02:46:43

char * const * 是一个指向 char 的指针。你只想要一个指向 char 的指针。

尝试:

int compare(const void *a, const void *b){
    return *(const char *) a - *(const char *) b;
}

此外,根据定义,sizeof(char) 始终等于 1。所以有些C程序员永远不会这样写出来。 (它是否使代码更易于阅读,或者只是表明您并不真正了解该语言,这是一个观点问题。我碰巧喜欢这种情况,但仅供参考。)

char * const * is a pointer to a pointer to char. You just want a pointer to char.

Try:

int compare(const void *a, const void *b){
    return *(const char *) a - *(const char *) b;
}

Also, sizeof(char) is always equal to 1, by definition. So some C programmers would never write it out like that. (It is a matter of opinion whether it makes the code easier to read or just signals that you do not really know the language. I happen to like it in this case, but just FYI.)

素衣风尘叹 2024-12-11 02:46:43

至少一开始,我只是更明确地逐行编写比较,以便于调试:

int compare(const void *a, const void *b)
{
    char* alpha = (char*)a;   // Might get const-warning on these lines, ignore for now.
    char* beta  = (char*)b;

    if (*alpha == *beta) return 0;
    if (*alpha < *beta) return -1;
    return 1;
}

这样,您可以更轻松地在调试器中观察它,添加输出行,并通常分解问题。

一旦它再次工作,您可以将其重新组合成一行紧凑的代码。

At least at first, I'd just write compare a bit more explicitly and line-by-line for easier debugging:

int compare(const void *a, const void *b)
{
    char* alpha = (char*)a;   // Might get const-warning on these lines, ignore for now.
    char* beta  = (char*)b;

    if (*alpha == *beta) return 0;
    if (*alpha < *beta) return -1;
    return 1;
}

This way, you can watch it in the debugger more easily, add output lines, and generally break the problem down.

Once its working again, you can re-combine it back into a single, compact line of code.

可可 2024-12-11 02:46:43

试试这个:

int compare(const void *a, const void *b){
  return *(const char *) a - *(const char *) b;
}

但是恕我直言,你根本不需要qsort

只需创建一个 int[256](或可能更少)的表并迭代该数组来记录每个字符的计数:

int res[256];
memset(res, 0, 256*sizeof(int));
int i;
for (i=0;i<lineCount;i++){
    res[tmp[i]]++;
}

这将提供 O(N) 与 qsort 的 O(NlogN)。

Try this:

int compare(const void *a, const void *b){
  return *(const char *) a - *(const char *) b;
}

But IMHO, you do not need qsort at all!

Just create a table of int[256] (or maybe less) and iterate the array to record the count of each character:

int res[256];
memset(res, 0, 256*sizeof(int));
int i;
for (i=0;i<lineCount;i++){
    res[tmp[i]]++;
}

This would provide O(N) vs qsort's O(NlogN).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文