From 13e0c9b5f58145acc7a542007526b72bbb4bc59f Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 6 Apr 2014 18:59:20 +0900 Subject: [PATCH] fix nth_element --- sprout/algorithm/cxx14/nth_element.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sprout/algorithm/cxx14/nth_element.hpp b/sprout/algorithm/cxx14/nth_element.hpp index 82b3f321..b971c165 100644 --- a/sprout/algorithm/cxx14/nth_element.hpp +++ b/sprout/algorithm/cxx14/nth_element.hpp @@ -132,24 +132,33 @@ namespace sprout { return; } do { + bool not_sorted_flag = false; if (n_swaps == 0) { if (nth < i) { j = m = first; while (++j != i) { if (comp(*j, *m)) { + not_sorted_flag = true; break; } m = j; } + if (not_sorted_flag) { + break; + } return; } else { j = m = i; while (++j != last) { if (comp(*j, *m)) { + not_sorted_flag = true; break; } m = j; } + if (not_sorted_flag) { + break; + } return; } }