From 200bb788e27282bbdf40fbafae48884f9467094d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 6 Feb 2015 16:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index a8df318..699f9ae 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -514,7 +514,6 @@ namespace TinySTL{ } template void sort(RandomIterator first, RandomIterator last, BinaryPredicate pred){ - static int n = 1; if (first >= last || first + 1 == last) return; if (last - first <= 20)//区间长度小于等于20的采用冒泡排序更快 @@ -522,15 +521,15 @@ namespace TinySTL{ auto mid = mid3(first, last - 1, pred); auto p1 = first, p2 = last - 2; while (p1 < p2){ - while (pred(*p1, mid) && p1 < p2) ++p1; - while (!pred(*p2, mid) && p1 < p2) --p2; + while (pred(*p1, mid) && (p1 < p2)) ++p1; + while (!pred(*p2, mid) && (p1 < p2)) --p2; if (p1 < p2){ swap(*p1, *p2); } } swap(*p1, *(last - 2));//将作为哨兵的mid item换回原来的位置 - sort(first, p1); - sort(p1 + 1, last); + sort(first, p1, pred); + sort(p1 + 1, last, pred); } }