Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 试图对结构中的数据进行排序,它排序正确,但最后一行是错误的_C_Arrays_Sorting_Struct_Insertion Sort - Fatal编程技术网

C 试图对结构中的数据进行排序,它排序正确,但最后一行是错误的

C 试图对结构中的数据进行排序,它排序正确,但最后一行是错误的,c,arrays,sorting,struct,insertion-sort,C,Arrays,Sorting,Struct,Insertion Sort,我输出的最后一行是错误的。请帮忙。我不知道为什么只有一行错误。您的代码在逻辑上不正确。如果您看到每次迭代的输出;您将获得以下输出 input 30.0 meters east, 70.0 meters north, power 0.0045 watts 53.0 meters east, 63.0 meters north, power 0.0006 watts 36.5 meters east, 27.0 meters north, power 0.0005 watts 70.0 meters

我输出的最后一行是错误的。请帮忙。我不知道为什么只有一行错误。

您的代码在逻辑上不正确。如果您看到每次迭代的输出;您将获得以下输出

input
30.0 meters east, 70.0 meters north, power 0.0045 watts
53.0 meters east, 63.0 meters north, power 0.0006 watts
36.5 meters east, 27.0 meters north, power 0.0005 watts
70.0 meters east, 25.0 meters north, power 0.0015 watts
20.0 meters east, 50.0 meters north, power 0.0008 watts

output
36.5 meters east, 27.0 meters north, power 0.0005 watts
53.0 meters east, 63.0 meters north, power 0.0006 watts
20.0 meters east, 50.0 meters north, power 0.0008 watts
70.0 meters east, 25.0 meters north, power 0.0015 watts
30.0 meters east, 70.0 meters north, power 0.0045 watts

wrong output generated by wrong code
36.5 meters east, 27.0 meters north, power 0.0005 watts
53.0 meters east, 63.0 meters north, power 0.0006 watts
20.0 meters east, 50.0 meters north, power 0.0008 watts
70.0 meters east, 25.0 meters north, power 0.0015 watts
20.0 meters east, 50.0 meters north, power 0.0045 watts
我想黄显才很容易猜出你错在哪里。 以下代码生成所需的输出

53.000000  63.000000  0.000600
53.000000  63.000000  0.004500
36.600000  27.000000  0.000500
70.000000  25.000000  0.001500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
36.600000  27.000000  0.004500
70.000000  25.000000  0.001500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
70.000000  25.000000  0.001500
70.000000  25.000000  0.004500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
20.000000  50.000000  0.000800
70.000000  25.000000  0.001500
20.000000  50.000000  0.004500
----------------------------
void排序(loc\u sos\u t a[],整数数组大小)
{
int i,j;
双指数;
结构位置索引块;
对于(i=0;i0&&a[j-1].sos>index;j--){
指数_块=a[j-1];
a[j-1]=a[i];
a[i]=索引块;
}
}
} 

这是插入排序的代码,对数组值的副本进行了最小修复(逻辑是正确的,您刚刚搞乱了数组元素的副本):


索引不应该从i=0开始吗?它将第二项与第一项进行比较。因此,如果我从0开始,我无法与索引-1进行比较,因为它不存在。非常感谢!我将使用修正后的效率较低的函数,因为qsort对我来说太高级了。谢谢你。一旦这个项目完成,我会回去学习这个qsort是如何工作的。非常感谢你!你是一个了不起的人,永远不会改变。谢谢你,谢谢你,每次迭代都能帮我把它分解。是的,我现在知道哪里出了问题了D:非常感谢@当您无法正确计算输出时,请始终尝试解决此类问题。这很有帮助。:)
53.000000  63.000000  0.000600
53.000000  63.000000  0.004500
36.600000  27.000000  0.000500
70.000000  25.000000  0.001500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
36.600000  27.000000  0.004500
70.000000  25.000000  0.001500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
70.000000  25.000000  0.001500
70.000000  25.000000  0.004500
20.000000  50.000000  0.000800
----------------------------
36.600000  27.000000  0.000500
53.000000  63.000000  0.000600
20.000000  50.000000  0.000800
70.000000  25.000000  0.001500
20.000000  50.000000  0.004500
----------------------------
void sort(loc_sos_t a[], int array_size)
{
   int i, j;
   double index;
   struct loc_sos_t index_block;

   for (i = 0; i < array_size; i++){
    index = a[i].sos;
    for (j = i-1; j > 0 && a[j-1].sos > index; j--){
       index_block= a[j-1];
       a[j-1]=a[i];
       a[i]=index_block;
    }
   }
} 
void sort(loc_sos_t a[], int array_size) {
    int i, j;
    double index;
    loc_sos_t index_block;
    for (i = 1; i < array_size; i++) {
        index = a[i].sos;
        index_block = a[i];
        for (j = i; j > 0 && a[j - 1].sos > index; j--) {
            a[j] = a[j - 1];
        }
        a[j] = index_block;
    }
}
int cmplocsos(const void *p1, const void *p2) {
    double s1 = ((loc_sos_t*)p1)->sos;
    double s2 = ((loc_sos_t*)p2)->sos;
    return (s1 > s2) - (s1 < s2);
}

qsort(&arr[0], array_size, sizeof(loc_sos_t), cmplocsos);