不是的qsort排序我的数组 [英] qSort not Sorting my array

查看:254
本文介绍了不是的qsort排序我的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

#include <stdio.h>
#include <stdlib.h>

float comp (const void * elem1, const void * elem2) {
    float f = *((float*)elem1);
    float s = *((float*)elem2);
    if (f > s) return  1;
    if (f < s) return -1;
    return 0;
}

int main(void) {
    int t, n, temp, temp1, x;
    float input[2][50][1000];
    scanf("%d", &t);
    for(temp=0; temp<t; temp++){
        scanf("%d ", &n);
        for(temp1=0; temp1<n; temp1++){
            scanf("%f", &input[0][temp][temp1]);
        }
        for(temp1=0; temp1<n; temp1++){
            scanf("%f", &input[1][temp][temp1]);
        }
        for(x=0; x<temp1; x++){
            printf("%f", input[0][temp][x]);
        }
        qsort (input[0][temp], n, sizeof(*input[0][temp]), comp);
        printf("\n Sorted Array:");
        for(x=0; x<temp1; x++){
            printf("%f", input[0][temp][x]);
        }

    }
    return 0;
}

和这里是我的输出:
0.7000000.2000000.800000
排序的数组:0.7000000.2000000.800000

and here is my output: 0.7000000.2000000.800000 Sorted Array:0.7000000.2000000.800000

谁能告诉为什么的qsort()不是为我工作?

Can anyone tell why is qsort() not working for me?

推荐答案

比较函数必须返回 INT (不是浮动)。所以,你必须做的是,改变这种:
浮动补偿(常量无效* elem1,常量无效* elem2时){ INT补偿(常量无效* elem1,常量无效* elem2时){这就是它! :)

Comparison functions must return int (not float). So what you have to do is, change this: float comp (const void * elem1, const void * elem2) { to int comp (const void * elem1, const void * elem2) { That's it! :)

这篇关于不是的qsort排序我的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆