Отсортировать элементы четных строк матрицы по возрастанию значения, а нечетных - по убыванию значения -
сортировать по замене и быструю сортировку
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");
}
#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");
}
Сергей Титей
Это у нас какой сортировка?
изучить Кнут Искусство программирования том 3 Сортировка и поиск
оно полезно.
оно полезно.
#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");
}
#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");
}
Похожие вопросы
- Проблемы с сортировкой массива методом пузырька.
- Выполните сортировку массивов ТРЕМЯ СПОСОБАМИ: методом пузырька, прямого поиска и быстрой сортировкой. С++
- Выполните сортировку массивов ТРЕМЯ СПОСОБАМИ: методом пузырька, прямого поиска и быстрой сортировкой. С++
- Сортировка массивов на С++
- Измерение времени на си. Почему-то скорость сортировки массива выводится со второго раза.. И еще надо измерить память
- КОНТРОЛЬНЫЕ ЗАДАНИЯ ПО СОРТИРОВКЕ МАССИВОВ
- Как сделать сортировку массива по убыванию, сохранив индексы на языке C
- Одномерные массивы. Сортировка
- Сортировка элементов массива методом Шелла
- Язык Си. Сортировка и поиск элементв массива