解读 qsort 行为
我需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
char * const *
是一个指向 char 的指针。你只想要一个指向 char 的指针。尝试:
此外,根据定义,
sizeof(char)
始终等于 1。所以有些C程序员永远不会这样写出来。 (它是否使代码更易于阅读,或者只是表明您并不真正了解该语言,这是一个观点问题。我碰巧喜欢这种情况,但仅供参考。)char * const *
is a pointer to a pointer to char. You just want a pointer to char.Try:
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.)至少一开始,我只是更明确地逐行编写比较,以便于调试:
这样,您可以更轻松地在调试器中观察它,添加输出行,并通常分解问题。
一旦它再次工作,您可以将其重新组合成一行紧凑的代码。
At least at first, I'd just write compare a bit more explicitly and line-by-line for easier debugging:
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.
试试这个:
但是恕我直言,你根本不需要
qsort
!只需创建一个 int[256](或可能更少)的表并迭代该数组来记录每个字符的计数:
这将提供 O(N) 与 qsort 的 O(NlogN)。
Try this:
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:
This would provide O(N) vs qsort's O(NlogN).