From 2dfdfe8a89e6efc861f38c28f0e7edb89157f6cc Mon Sep 17 00:00:00 2001 From: Liam Huang Date: Wed, 17 Oct 2018 09:37:30 +0800 Subject: [PATCH] [cpp][11_sorts] selection_sort, updated [prevent useless swap]. --- c-cpp/11_sorts/sorts.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/c-cpp/11_sorts/sorts.hpp b/c-cpp/11_sorts/sorts.hpp index dc965ba..15b41f2 100644 --- a/c-cpp/11_sorts/sorts.hpp +++ b/c-cpp/11_sorts/sorts.hpp @@ -53,7 +53,9 @@ void selection_sort(BidirIt first, tag = itt; } } - std::swap(*it, *tag); + if (tag != it) { + std::swap(*it, *tag); + } } }