简体   繁体   English

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

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

Im trying to sort, using Insertion Sort, variable-length array of pointers to struct objects.我试图使用插入排序对结构对象的可变长度指针数组进行排序。

The sorting criteria is based on the structs distance_to_neighbor attribute.排序标准基于 structs distance_to_neighbor 属性。

The problem is it seems that sorted output is semi-sorted.问题是似乎排序的 output 是半排序的。

Here is my data structure for a tree node:这是我的树节点的数据结构:

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

Here is my Insertion Sort implementation, relevant code snippet (based on https://www.techiedelight.com/insertion-sort-iterative-recursive/ ):这是我的插入排序实现,相关代码片段(基于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;
    }
}

Code snippet used for debugging before & after sort:用于排序前后调试的代码片段:

printf ("debug {"); printf(“调试{”); float * info;浮动 * 信息; float distance = 0;浮动距离 = 0; for (int c = 0; c < k_dimensions; c++) { info = (float *) current->info; 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);

Here are the values before sorting (should be sorted based on distance_to_neighbor):以下是排序前的值(应根据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

After sorting (looks sorted descending order then suddenly ascending order.: It should only be ascending order):排序后(看起来是降序排序,然后突然升序。:它应该只是升序):

{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

I must keep my function signature the same as void insertion_sort(tree ** arr, int n) .我必须保持我的 function 签名与void insertion_sort(tree ** arr, int n)相同。 How can I fix this sorting bug?如何修复此排序错误?

Thanks谢谢

You seem to assume that pre-value changes as you change j , but it needs to be re-calculated with each change to j .您似乎假设当您更改jpre-value会发生变化,但每次更改j时都需要重新计算它。

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

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