Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <algorithm>
- #include <iostream>
- using namespace std;
- void read_array(int* x, int x_size) {
- for (int i = 0; i < x_size; ++i) {
- scanf("%d", &x[i]);
- }
- }
- void write_array(int* x, int x_size) {
- for (int i = 0; i < x_size; ++i) {
- printf("%d ", x[i]);
- }
- }
- void sort_array(int* x, int x_size) {
- if (x_size == 1)
- return;
- int a_size = x_size / 2;
- int b_size = x_size - a_size;
- int* b = x + a_size;
- int* c = new int[x_size];
- int a_i = 0;
- int b_i = 0;
- sort_array(x, a_size);
- sort_array(b, b_size);
- for (int c_i = 0; c_i < x_size; ++c_i) {
- if (a_i == a_size) {
- c[c_i] = b[b_i];
- b_i += 1;
- continue;
- }
- if (b_i == b_size) {
- c[c_i] = x[a_i];
- a_i += 1;
- continue;
- }
- if (x[a_i] >= b[b_i]) {
- c[c_i] = b[b_i];
- b_i += 1;
- }
- else {
- c[c_i] = x[a_i];
- a_i += 1;
- }
- }
- for (int i = 0; i < x_size; ++i) {
- x[i] = c[i];
- }
- delete[] c;
- }
- int main() {
- int n;
- cin >> n;
- int* a = new int[n];
- read_array(a, n);
- sort_array(a, n);
- write_array(a, n);
- delete[]a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement