From 1fe60d64c18c8d6dd7e61c9b45f2f6648e509e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 7 Feb 2015 00:42:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96bubble=5Fsort?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index cdeaba6..b1c8333 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -501,10 +501,15 @@ namespace TinySTL{ void bubble_sort(RandomIterator first, RandomIterator last, BinaryPredicate pred){ auto len = last - first; for (auto i = len; i != 0; --i){ + bool swaped = false; for (auto p = first; p != (first + i - 1); ++p){ - if (pred(*(p + 1), *p)) + if (pred(*(p + 1), *p)){ swap(*(p + 1), *p); + swaped = true; + } } + if (!swaped) + break; } } }