From 25115609ffd3674672ec6a4cdce3462729414b58 Mon Sep 17 00:00:00 2001 From: Mike Precup Date: Wed, 9 Jul 2025 15:12:20 -0700 Subject: [PATCH] Fix issue with array comparison reference --- core/templates/sort_array.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/templates/sort_array.h b/core/templates/sort_array.h index 2afa6576783..9bfee2991cc 100644 --- a/core/templates/sort_array.h +++ b/core/templates/sort_array.h @@ -168,17 +168,17 @@ public: inline int64_t partitioner(int64_t p_first, int64_t p_last, int64_t p_pivot, T *p_array) const { const int64_t unmodified_first = p_first; const int64_t unmodified_last = p_last; - const T &pivot_element = p_array[p_pivot]; + const T *pivot_element_location = &p_array[p_pivot]; while (true) { - while (p_first != p_pivot && compare(p_array[p_first], pivot_element)) { + while (p_first != p_pivot && compare(p_array[p_first], *pivot_element_location)) { if constexpr (Validate) { ERR_BAD_COMPARE(p_first == unmodified_last - 1); } p_first++; } p_last--; - while (p_last != p_pivot && compare(pivot_element, p_array[p_last])) { + while (p_last != p_pivot && compare(*pivot_element_location, p_array[p_last])) { if constexpr (Validate) { ERR_BAD_COMPARE(p_last == unmodified_first); } @@ -189,6 +189,11 @@ public: return p_first; } + if (pivot_element_location == &p_array[p_first]) { + pivot_element_location = &p_array[p_last]; + } else if (pivot_element_location == &p_array[p_last]) { + pivot_element_location = &p_array[p_first]; + } SWAP(p_array[p_first], p_array[p_last]); p_first++; }