From e6fc72011fb6bf2db62e2d0072b7dd007d515399 Mon Sep 17 00:00:00 2001 From: jaimefrio Date: Tue, 11 Mar 2014 00:47:15 -0700 Subject: [PATCH] BUG: wrong search for pivot in `quicker_sort` This PR corrects an error in the search for the existence of a distinct pivot for sorting in the `quicker_sort` routines. This could lead to `quicker_sort` wrongly believing that all items in a subarray are equal and hence require no sorting. While this doesn't explain #835 and #902, it may be related. --- skimage/exposure/unwrap_2d_ljmu.c | 72 +++++++++++++++---------------- skimage/exposure/unwrap_3d_ljmu.c | 2 +- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/skimage/exposure/unwrap_2d_ljmu.c b/skimage/exposure/unwrap_2d_ljmu.c index 6be7966a..024eaaa6 100644 --- a/skimage/exposure/unwrap_2d_ljmu.c +++ b/skimage/exposure/unwrap_2d_ljmu.c @@ -77,64 +77,64 @@ typedef enum {yes, no} yes_no; yes_no find_pivot(EDGE *left, EDGE *right, double *pivot_ptr) { - EDGE a, b, c, *p; + EDGE a, b, c, *p; - a = *left; - b = *(left + (right - left) /2 ); - c = *right; - o3(a,b,c); + a = *left; + b = *(left + (right - left) / 2); + c = *right; + o3(a, b, c); - if (a.reliab < b.reliab) + if (a.reliab < b.reliab) { - *pivot_ptr = b.reliab; - return yes; + *pivot_ptr = b.reliab; + return yes; } - if (b.reliab < c.reliab) + if (b.reliab < c.reliab) { - *pivot_ptr = c.reliab; - return yes; + *pivot_ptr = c.reliab; + return yes; } - for (p = left + 1; p <= right; ++p) + for (p = left + 1; p <= right; ++p) { - if (p->reliab != left->reliab) - { - *pivot_ptr = (p->reliab < left->reliab) ? left->reliab : p->reliab; - return yes; - } - return no; + if (p->reliab != left->reliab) + { + *pivot_ptr = (p->reliab < left->reliab) ? left->reliab : p->reliab; + return yes; + } } + return no; } EDGE *partition(EDGE *left, EDGE *right, double pivot) { - while (left <= right) + while (left <= right) { - while (left->reliab < pivot) - ++left; - while (right->reliab >= pivot) - --right; - if (left < right) - { - swap (*left, *right); - ++left; - --right; - } + while (left->reliab < pivot) + ++left; + while (right->reliab >= pivot) + --right; + if (left < right) + { + swap (*left, *right); + ++left; + --right; + } } - return left; + return left; } void quicker_sort(EDGE *left, EDGE *right) { - EDGE *p; - double pivot; + EDGE *p; + double pivot; - if (find_pivot(left, right, &pivot) == yes) + if (find_pivot(left, right, &pivot) == yes) { - p = partition(left, right, pivot); - quicker_sort(left, p - 1); - quicker_sort(p, right); + p = partition(left, right, pivot); + quicker_sort(left, p - 1); + quicker_sort(p, right); } } //--------------end quicker_sort algorithm ----------------------------------- diff --git a/skimage/exposure/unwrap_3d_ljmu.c b/skimage/exposure/unwrap_3d_ljmu.c index 1874c051..4f23f705 100644 --- a/skimage/exposure/unwrap_3d_ljmu.c +++ b/skimage/exposure/unwrap_3d_ljmu.c @@ -104,8 +104,8 @@ yes_no find_pivot(EDGE *left, EDGE *right, double *pivot_ptr) *pivot_ptr = (p->reliab < left->reliab) ? left->reliab : p->reliab; return yes; } - return no; } + return no; } EDGE *partition(EDGE *left, EDGE *right, double pivot)