在整数数组上实现 qsort 时遇到问题,无法处理超过 6 个数字的数组
Having trouble implementing qsort on an array of integers, not working on arrays with more than 6 numbers
作为练习,我们的讲师正在让我们实施一些排序方法。两个我们自己构建,我做得很好,讲师给了我们 qsort 的代码,如下所示:
#include <stdio.h>
#include <stdlib.h>
//Your comparison function, here comparing "short" type data.
int larger_than(const void* left, const void* right) {
short* left_s = (short*) left;
short* right_s = (short*) right;
return *left_s > *right_s;
}
int main() {
int item_count = 5;
short* array = calloc(item_count, sizeof(short));
array[0] = 4;
array[1] = 0;
array[2] = 1;
array[3] = 13;
array[4] = 4;
qsort(array, item_count, sizeof(short), larger_than);
//Name of your comparison function --------^
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
free(array);
return 0;
}
因此我们必须对其进行调整以对整数数组进行排序,并且不是手动填充数组,而是在标准输入中指定数组长度以及填充数组的整数。所以我的代码看起来像这样:
#include <stdio.h>
#include <stdlib.h>
int larger_than(const void* left, const void* right) {
int* left_s = (int*) left;
int* right_s = (int*) right;
return *left_s > *right_s;
}
void print_array(int* data_array, int array_length) {
int counter = 0;
while (counter != array_length) {
printf("%d ", data_array[counter]);
counter = counter + 1;
}
}
int main() {
int input_size;
int data_item;
scanf("%d", &input_size);
int* data_array = calloc(input_size, sizeof(int));
int counter = 0;
while (counter != input_size) {
scanf("%d", &data_item);
data_array[counter] = data_item;
counter = counter + 1;
}
qsort(data_array, input_size, sizeof(int), larger_than);
print_array(data_array, input_size);
free(data_array);
return 0;
}
我最初使用的是在线 IDE (ideone),它编译得很好,并给出了正确的排序结果。然后我用 GCC 编译,发现它给了我一个错误的排序。所以现在我想弄清楚我的代码有什么问题。例如,如果我给我的程序输入:
7
7 6 5 4 3 2 1
它returns
4 3 2 1 5 6 7
而不是
1 2 3 4 5 6 7
我做了一些更多的测试,发现了一些非常奇怪的东西,如果我给它 6 个数字,它会很好地排序,但是任何超过 6 个都会给我一个完全错误的结果。我是 C 的新手,所以我一直在查看我的代码,但终究无法弄清楚我做错了什么。有什么建议吗?
您的自定义比较函数 return如果第一个参数较大,则为 1;如果两个参数相等或第二个更大,则为 0。
将其与比较函数应该return(来自man qsort
)进行比较:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
如果这确实来自您的教授,您应该让他或她参考联机帮助页。
作为练习,我们的讲师正在让我们实施一些排序方法。两个我们自己构建,我做得很好,讲师给了我们 qsort 的代码,如下所示:
#include <stdio.h>
#include <stdlib.h>
//Your comparison function, here comparing "short" type data.
int larger_than(const void* left, const void* right) {
short* left_s = (short*) left;
short* right_s = (short*) right;
return *left_s > *right_s;
}
int main() {
int item_count = 5;
short* array = calloc(item_count, sizeof(short));
array[0] = 4;
array[1] = 0;
array[2] = 1;
array[3] = 13;
array[4] = 4;
qsort(array, item_count, sizeof(short), larger_than);
//Name of your comparison function --------^
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
free(array);
return 0;
}
因此我们必须对其进行调整以对整数数组进行排序,并且不是手动填充数组,而是在标准输入中指定数组长度以及填充数组的整数。所以我的代码看起来像这样:
#include <stdio.h>
#include <stdlib.h>
int larger_than(const void* left, const void* right) {
int* left_s = (int*) left;
int* right_s = (int*) right;
return *left_s > *right_s;
}
void print_array(int* data_array, int array_length) {
int counter = 0;
while (counter != array_length) {
printf("%d ", data_array[counter]);
counter = counter + 1;
}
}
int main() {
int input_size;
int data_item;
scanf("%d", &input_size);
int* data_array = calloc(input_size, sizeof(int));
int counter = 0;
while (counter != input_size) {
scanf("%d", &data_item);
data_array[counter] = data_item;
counter = counter + 1;
}
qsort(data_array, input_size, sizeof(int), larger_than);
print_array(data_array, input_size);
free(data_array);
return 0;
}
我最初使用的是在线 IDE (ideone),它编译得很好,并给出了正确的排序结果。然后我用 GCC 编译,发现它给了我一个错误的排序。所以现在我想弄清楚我的代码有什么问题。例如,如果我给我的程序输入:
7
7 6 5 4 3 2 1
它returns
4 3 2 1 5 6 7
而不是
1 2 3 4 5 6 7
我做了一些更多的测试,发现了一些非常奇怪的东西,如果我给它 6 个数字,它会很好地排序,但是任何超过 6 个都会给我一个完全错误的结果。我是 C 的新手,所以我一直在查看我的代码,但终究无法弄清楚我做错了什么。有什么建议吗?
您的自定义比较函数 return如果第一个参数较大,则为 1;如果两个参数相等或第二个更大,则为 0。
将其与比较函数应该return(来自man qsort
)进行比较:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
如果这确实来自您的教授,您应该让他或她参考联机帮助页。