繁体   English   中英

C 使用插入排序对指向结构的指针数组进行排序

[英]C Using insertion sort to sort array of pointers to structs

我试图使用插入排序对结构对象的可变长度指针数组进行排序。

排序标准基于 structs distance_to_neighbor 属性。

问题是似乎排序的 output 是半排序的。

这是我的树节点的数据结构:

typedef struct tree_
    {
        struct tree *left; 
        struct tree *right; 
        float * info;  
        float distance_to_neighbor;
    } tree;

这是我的插入排序实现,相关代码片段(基于https://www.techiedelight.com/insertion-sort-iterative-recursive/ ):

// perform insertion sort on array of references to structs
void insertion_sort(tree ** arr, int n)
{
    // Start from second element (element at index 0 
    // is already sorted)
    tree * pre_value = NULL;
    for (int i = 1; i < n; i++) 
    {
        tree * value = *(arr + i);
        int j = i;

        // Find the index j within the sorted subset arr[0..i-1]
        // where element arr[i] belongs
        pre_value = *(arr + j - 1);
        while (j > 0 && pre_value->distance_to_neighbor > value.distance_to_neighbor) 
        {
            **(arr + j) = **(arr + j - 1);
            j--;
        }

        // Note that subarray arr[j..i-1] is shifted to
        // the right by one position i.e. arr[j+1..i]
        **(arr + j) = value;
    }
}

用于排序前后调试的代码片段:

printf(“调试{”); 浮动 * 信息; 浮动距离 = 0; for (int c = 0; c < k_dimensions; c++) { info = (float *) current->info;

                if (NULL != info)
                {

                    printf ("%f,", info[c]);
                }
                else
                {
                    break;
                }

            }//end for 
            printf ("} ");
            distance = (float) current->distance_to_neighbor;
            printf ("distance_to_neighbor=%f\n", distance);

以下是排序前的值(应根据distance_to_neighbor排序):

debug {-50.000000,-50.000000,-50.000000,} distance_to_neighbor=53.000000
debug {-3.000000,-3.000000,-3.000000,} distance_to_neighbor=6.000000
debug {-2.000000,-2.000000,-2.000000,} distance_to_neighbor=5.000000
debug {-1.000000,-1.000000,-1.000000,} distance_to_neighbor=4.000000
debug {0.000000,0.000000,0.000000,} distance_to_neighbor=3.000000
debug {1.000000,1.000000,1.000000,} distance_to_neighbor=2.000000
debug {2.000000,2.000000,2.000000,} distance_to_neighbor=1.000000
debug {3.000000,3.000000,3.000000,} distance_to_neighbor=0.000000
debug {4.000000,4.000000,4.000000,} distance_to_neighbor=1.000000
debug {5.000000,5.000000,5.000000,} distance_to_neighbor=2.000000
debug {6.000000,6.000000,6.000000,} distance_to_neighbor=3.000000
debug {7.000000,7.000000,7.000000,} distance_to_neighbor=4.000000
debug {8.000000,8.000000,8.000000,} distance_to_neighbor=5.000000
debug {100.000000,100.000000,100.000000,} distance_to_neighbor=97.000000

排序后(看起来是降序排序,然后突然升序。:它应该只是升序):

{8.000000,8.000000,8.000000,} distance_to_neighbor=5.000000
{7.000000,7.000000,7.000000,} distance_to_neighbor=4.000000
{6.000000,6.000000,6.000000,} distance_to_neighbor=3.000000
{5.000000,5.000000,5.000000,} distance_to_neighbor=2.000000
{4.000000,4.000000,4.000000,} distance_to_neighbor=1.000000
{3.000000,3.000000,3.000000,} distance_to_neighbor=0.000000
{2.000000,2.000000,2.000000,} distance_to_neighbor=1.000000
{1.000000,1.000000,1.000000,} distance_to_neighbor=2.000000
{0.000000,0.000000,0.000000,} distance_to_neighbor=3.000000
{-1.000000,-1.000000,-1.000000,} distance_to_neighbor=4.000000
{-2.000000,-2.000000,-2.000000,} distance_to_neighbor=5.000000
{-3.000000,-3.000000,-3.000000,} distance_to_neighbor=6.000000
{-50.000000,-50.000000,-50.000000,} distance_to_neighbor=53.000000
{100.000000,100.000000,100.000000,} distance_to_neighbor=97.000000

我必须保持我的 function 签名与void insertion_sort(tree ** arr, int n)相同。 如何修复此排序错误?

谢谢

您似乎假设当您更改jpre-value会发生变化,但每次更改j时都需要重新计算它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM