From 9b53b8e935a648e225508e51f45d83dfaeacd896 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Wed, 27 Apr 2022 14:04:40 +0400 Subject: =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BB?= =?UTF-8?q?=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BF=D1=80=D1=8F=D0=BC=D1=8B=D0=BC=20=D0=B2=D1=8B=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=BC=20=D0=BD=D0=B0=20c++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- straight-selection.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 straight-selection.cpp (limited to 'straight-selection.cpp') diff --git a/straight-selection.cpp b/straight-selection.cpp new file mode 100644 index 0000000..ff5a7b6 --- /dev/null +++ b/straight-selection.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +template void +straight_selection(std::vector &array) +{ + for (int i = 0; i < array.size(); ++i) + { + int k = i; + int x = array[i]; + for (int j = i; j < array.size(); ++j) + { + if (array[j] < x) + { + k = j; + x = array[j]; + } + } + array[k] = array[i]; + array[i] = x; + } +} + +template std::ostream & +operator<<(std::ostream &stream, std::vector &v) +{ + if (v.size() == 0) stream << "{ }"; + + stream << "array = { "; + for (int i = 0; i < int(v.size()) - 1; ++i) + stream << v[i] << ", "; + + stream << v.back() << " }"; + return stream; +} + +int +main() +{ + std::vector array = { 2, 7, 12, 30, 11, 4, 12, 5, 20 }; + std::cout << array << std::endl; + straight_selection(array); + std::cout << array << std::endl; + + return 0; +} -- cgit v1.2.3