Deciphering qsort behavior
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开发者_C百科 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.
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.)
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.
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).
精彩评论