c - 冒泡排序问题

标签 c debugging sorting pointers bubble-sort

gcc 编译以下代码没有错误。我正在创建一个冒泡排序函数,它可以用于任何数据类型的数组(因此是函数指针)。

它可以毫无问题地对字符串数组 (arr2) 进行排序,但是,我不明白为什么它不能正确地对整数数组 (arr) 进行排序。我在 compare_long 函数中添加了一条 printf 语句,以查看发生了什么。看起来整数没有正确传递给它。任何帮助将不胜感激。


    #include <stdio.h>
    #include <string.h>

    #define MAX_BUF 256

    long arr[10] = { 3,6,1,2,3,8,4,1,7,2};
    char arr2[5][20] = { "Mickey Mouse",
                         "Donald Duck",
                         "Minnie Mouse",
                         "Goofy",
                         "Pluto" };

    void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *));
    int compare_string(const void *m, const void *n);
    int compare_long(const void *m, const void *n);

    int main(void) {
            int i;
            puts("\nBefore Sorting:\n");

            for(i = 0; i < 10; i++) {              /* show the long ints */
                    printf("%ld ",arr[i]);
            }
            puts("\n");

            for(i = 0; i < 5; i++) {               /* show the strings */
                    printf("%s\n", arr2[i]);
            }

            bubble(arr, 4, 10, compare_long);      /* sort the longs */
            bubble(arr2, 20, 5, compare_string);   /* sort the strings */
            puts("\n\nAfter Sorting:\n");

            for(i = 0; i < 10; i++) {              /* show the sorted longs */
                    printf("%d ",arr[i]);
            }
            puts("\n");

            for(i = 0; i < 5; i++) {               /* show the sorted strings */
                    printf("%s\n", arr2[i]);
            }
            return 0;
    }

    void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *)) {

            int i, j, k;
            unsigned char buf[MAX_BUF];
            unsigned char *bp = p;

            for(i = N - 1; i >= 0; i--) {

                    for(j = 1; j <= i; j++) {     

                            k = fptr((void *)(bp + width*(j-1)), (void *)(bp + j*width));

                            if(k > 0) {
                                    memcpy(buf, bp + width*(j-1), width);
                                    memcpy(bp + width*(j-1), bp + j*width , width);
                                    memcpy(bp + j*width, buf, width);
                            }
                    }
            }
    }

    int compare_string(const void *m, const void *n) {
            char *m1 = (char *)m;
            char *n1 = (char *)n;
            return (strcmp(m1,n1));
    }

    int compare_long(const void *m, const void *n) {
            long *m1, *n1;
            m1 = (long *)m;
            n1 = (long *)n;

            printf("m1 = %l and n1 = %l\n", *m1, *n1);

            return (*m1 > *n1);
    }

最佳答案

ANSI C 规范将 long 定义为最少 4 个字节(32 位),但 GCC 在您的情况下将 long 定义为 8 个字节。它是特定于体系结构的,因此如果您需要特定大小,则需要使用 sizeof(long) 或 C99 类型之一,如 uint32_t 或 int32_t。

关于c - 冒泡排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10344940/

相关文章:

c - 没有得到正确的总和 - openmp

c - 操作数与赋值不兼容

c - 命名管道,使用 Fork()

Javascript - if 和 else 语句都执行

c++ - 为什么 LeetCode 给出错误 : AddressSanitizer: heap-buffer-overflow?

c - 使用 libpng 过滤器

c++ - 调试boost::thread应用,误报率高

debugging - 调试 makefile 的工具

swift - 按距离排序 Tableview,Swift

arrays - 确定一个非常大的数组是否包含重复项