C/C++

С++ Сортировка массива

Отсортировать элементы четных строк матрицы по возрастанию значения, а нечетных - по убыванию значения -
сортировать по замене и быструю сортировку
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <array>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
const auto rand = [&] { return uid(gen); };
const auto show = [](int x) { cout << setw(3) << x; };
array<array<int, 10>, 8> matrix;
for (auto& row : matrix) generate(row.begin(), row.end(), rand);
for (auto& row : matrix) {
for_each(row.begin(), row.end(), show);
puts("");
}
puts("");
auto n = 0;
for (auto& row : matrix) {
if (++n & 1) sort(row.begin(), row.end(), greater<>());
else sort(row.begin(), row.end());
}
for (auto& row : matrix) {
for_each(row.begin(), row.end(), show);
puts("");
}
system("pause > nul");
}
СИ
Сергей Исаев
88 758
Лучший ответ
Сергей Титей Это у нас какой сортировка?
изучить Кнут Искусство программирования том 3 Сортировка и поиск
оно полезно.
Elyor Nishanov
Elyor Nishanov
36 668
#include
#include
#include
#include
#include
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
const auto rand = [&] { return uid(gen); };
const auto show = [](int x) { cout << setw(3) << x; };
array<array, 8> matrix;
for (auto& row : matrix) generate(row.begin(), row.end(), rand);
for (auto& row : matrix) {
for_each(row.begin(), row.end(), show);
puts("");
}
puts("");
auto n = 0;
for (auto& row : matrix) {
if (++n & 1) sort(row.begin(), row.end(), greater<>());
else sort(row.begin(), row.end());
}
for (auto& row : matrix) {
for_each(row.begin(), row.end(), show);
puts("");
}
system("pause > nul");
}