summaryrefslogtreecommitdiff
path: root/straight-selection.cpp
diff options
context:
space:
mode:
authorAndrew Guschin <guschin.drew@gmail.com>2022-04-27 14:04:40 +0400
committerAndrew Guschin <guschin.drew@gmail.com>2022-04-27 14:04:40 +0400
commit9b53b8e935a648e225508e51f45d83dfaeacd896 (patch)
tree3d10c862f19d04814cc9a63abef91cfa707c3ce1 /straight-selection.cpp
parentca510e0f9f3da8560ac72a4c80b33b167582e5e5 (diff)
Переписал сортировку прямым выбором на c++
Diffstat (limited to 'straight-selection.cpp')
-rw-r--r--straight-selection.cpp47
1 files changed, 47 insertions, 0 deletions
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 <iostream>
+#include <vector>
+#include <ostream>
+
+template<class T> void
+straight_selection(std::vector<T> &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<class T> std::ostream &
+operator<<(std::ostream &stream, std::vector<T> &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<int> 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;
+}