From 4e24903b18986a9cd6f21264b34cb5fe91b8553b Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Tue, 18 Apr 2023 16:22:01 +0200 Subject: [PATCH] quicksort was buggy lol --- thiersort.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/thiersort.h b/thiersort.h index 6702a2d..c010340 100644 --- a/thiersort.h +++ b/thiersort.h @@ -477,9 +477,16 @@ static inline void ts_quicksort_inplace( } } + /* Fix left to be after the point of separation in odd lengths */ + if(left == right) { + tskey leftkey = arr[left].key; + TSU32 lefti = arr[left].i; + left += (lt(pivotkey, leftkey, pivoti, lefti, reent_data)); + } + /* Just simple recursion for now */ - ts_quicksort_inplace(arr, from, mid, lt, reent_data); - ts_quicksort_inplace(arr, mid, to, lt, reent_data); + ts_quicksort_inplace(arr, from, left, lt, reent_data); + ts_quicksort_inplace(arr, left, to, lt, reent_data); } /** @@ -516,9 +523,9 @@ static inline void ts_quicksort_fromto( } } - /* Just simple recursion for now */ - ts_quicksort_inplace(dst, from, mid, lt, reent_data); - ts_quicksort_inplace(dst, mid, to, lt, reent_data); + /* Can use inplace steps from now on */ + ts_quicksort_inplace(dst, from, li, lt, reent_data); + ts_quicksort_inplace(dst, li, to, lt, reent_data); } /** Internal code reuse (type-branches should be optimized out) */