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.c | 44 -------------------------------------------- straight-selection.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 44 deletions(-) delete mode 100644 straight-selection.c create mode 100644 straight-selection.cpp diff --git a/straight-selection.c b/straight-selection.c deleted file mode 100644 index f05d098..0000000 --- a/straight-selection.c +++ /dev/null @@ -1,44 +0,0 @@ -#include - -void -straight_selection(int *array, size_t size) -{ - for (int i = 0; i < size; ++i) - { - int k = i; - int x = array[i]; - for (int j = i; j < size; ++j) - { - if (array[j] < x) - { - k = j; - x = array[j]; - } - } - array[k] = array[i]; - array[i] = x; - } -} - -void -print_array(int *array, size_t size) -{ - if (size == 0) printf("array = { }\n"); - - printf("array = { "); - for (int i = 0; i < size - 1; ++i) - printf("%i, ", array[i]); - printf("%i };\n", array[size - 1]); -} - -int -main() -{ - int array[] = { 2, 7, 12, 30, 11, 4, 12, 5, 20 }; - size_t size = sizeof(array) / sizeof(*array); - print_array(array, size); - straight_selection(array, size); - print_array(array, size); - - return 0; -} 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