Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std;
  5. void read_array(int* x, int x_size) {
  6.     for (int i = 0; i < x_size; ++i) {
  7.         scanf("%d", &x[i]);
  8.     }
  9. }
  10. void write_array(int* x, int x_size) {
  11.     for (int i = 0; i < x_size; ++i) {
  12.         printf("%d ", x[i]);
  13.     }
  14. }
  15. void sort_array(int* x, int x_size) {
  16.     if (x_size == 1)
  17.         return;
  18.  
  19.     int a_size = x_size / 2;
  20.     int b_size = x_size - a_size;
  21.     int* b = x + a_size;
  22.     int* c = new int[x_size];
  23.     int a_i = 0;
  24.     int b_i = 0;
  25.     sort_array(x, a_size);
  26.     sort_array(b, b_size);
  27.     for (int c_i = 0; c_i < x_size; ++c_i) {
  28.         if (a_i == a_size) {
  29.             c[c_i] = b[b_i];
  30.             b_i += 1;
  31.             continue;
  32.         }
  33.         if (b_i == b_size) {
  34.             c[c_i] = x[a_i];
  35.             a_i += 1;
  36.             continue;
  37.         }
  38.         if (x[a_i] >= b[b_i]) {
  39.             c[c_i] = b[b_i];
  40.             b_i += 1;
  41.         }
  42.         else {
  43.             c[c_i] = x[a_i];
  44.             a_i += 1;
  45.         }
  46.     }
  47.     for (int i = 0; i < x_size; ++i) {
  48.         x[i] = c[i];
  49.     }
  50.     delete[] c;
  51. }
  52. int main() {
  53.     int n;
  54.     cin >> n;
  55.     int* a = new int[n];
  56.     read_array(a, n);
  57.     sort_array(a, n);
  58.     write_array(a, n);
  59.     delete[]a;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement