/
fluffymax2005
/
algorithms
Обзор
Документация
Войти
/
fluffymax2005
/
algorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
quick_sort.cpp
60 строк
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 QuickSort { public: static void sort(T *arr, const size_t length) { sort_recursion(arr, 0, length - 1); } 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] = std::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 void sort_recursion(T *arr, const int64_t low, const int64_t high) { if (low < high) { size_t pivot_index = partition(arr, low, high); sort_recursion(arr, low, pivot_index - 1); sort_recursion(arr, pivot_index + 1, high); } } static size_t partition(T *arr, size_t low, size_t high) { const T pivot_value = arr[(low + high) / 2]; std::swap(arr[(low + high) / 2], arr[high]); size_t i = low; for (size_t j = low; j < high; ++j) { if (arr[j] <= pivot_value) { std::swap(arr[i], arr[j]); ++i; } } std::swap(arr[i], arr[high]); return i; } };