/
AirLexa
/
06
Обзор
Документация
Войти
/
AirLexa
/
06
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lesson_03/Task_1/Task_1.cpp
67 строк
1 KB
AirLexa
урок 3
08 июн 2026, 15:25
Верифицирован
08 июн 2026, 15:25
e290502
Код
Авторство
О чём код?
#include <iostream> #include <windows.h> #include <vector> #include <future> #include <thread> #include <algorithm> void findMinAsync(const std::vector<int>& arr, size_t start, std::promise<size_t> promise) { size_t minIndex = start; for (size_t i = start + 1; i < arr.size(); ++i) { if (arr[i] < arr[minIndex]) { minIndex = i; } } promise.set_value(minIndex); } void selectionSortAsync(std::vector<int>& arr) { for (size_t i = 0; i < arr.size() - 1; ++i) { std::promise<size_t> promise; std::future<size_t> future = promise.get_future(); std::thread worker(findMinAsync, std::cref(arr), i, std::move(promise)); size_t minIndex = future.get(); worker.join(); std::swap(arr[i], arr[minIndex]); } } void print(const std::vector<int>& arr) { for (int value : arr) { std::cout << value << " "; } std::cout << std::endl; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); std::vector<int> numbers = { 9, 3, 8, 1, 6, 2, 7, 5, 4 }; std::cout << "Исходный массив:" << std::endl; print(numbers); selectionSortAsync(numbers); std::cout << "\nПосле сортировки выбором:" << std::endl; print(numbers); return 0; }