Reputation: 2994
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.
The problem is it seems that sorted output is semi-sorted.
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/):
// 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 {"); float * info; float distance = 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);
Here are the values before sorting (should be sorted based on 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)
. How can I fix this sorting bug?
Thanks
Upvotes: 2
Views: 522
Reputation: 49813
You seem to assume that pre-value
changes as you change j
, but it needs to be re-calculated with each change to j
.
Upvotes: 1