/
fluffymax2005
/
algorithms
Обзор
Документация
Войти
/
fluffymax2005
/
algorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
selection_sort.cpp
51 строка
1 KB
fluffymax2005
Init commit for initial algorithms
16 фев 2026, 10:37
16 фев 2026, 10:37
85155db
Код
Авторство
О чём код?
#include <iostream> #include <new> #include <ostream> #include <stdlib.h> template <typename T> class SelectionSort { public: static void sort(T *arr, const size_t length) { if (arr == nullptr || length < 1) return; for (auto i = 0; i < length - 1; ++i) for (auto j = i + 1; j < length; ++j) { size_t smallestIndex = findSmallestIndex(arr, length, i); std::swap(arr[smallestIndex], arr[i]); } } static T *createArray(const size_t length) { T *arr = new (std::nothrow) T[length]; if (arr) { for (size_t i = 0; i < length; ++i) { arr[i] = rand() % 65536; } } return arr; } static void printArray(T *arr, size_t length, std::ostream &os = std::cout) { if (arr == nullptr || length < 1) return; os << "Arr = ["; for (auto i = 0; i < length; ++i) { if (i == length - 1) os << arr[i] << "]"; else os << arr[i] << ", "; } } private: static size_t findSmallestIndex(T *arr, const size_t length, const size_t offset) { size_t min = offset; for (size_t i = offset; i < length; ++i) { if (arr[i] < arr[min]) min = i; } return min; } };