Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 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,int& x_c) {
  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,x_c);
  26.     sort_array(b, b_size,x_c);
  27.     for (int c_i = 0; c_i < x_size; ++c_i) {
  28.         x_c += 1;
  29.         if (a_i == a_size) {
  30.             c[c_i] = b[b_i];
  31.             b_i += 1;
  32.             continue;
  33.         }
  34.         if (b_i == b_size) {
  35.             c[c_i] = x[a_i];
  36.             a_i += 1;
  37.             continue;
  38.         }
  39.         if (x[a_i] >= b[b_i]) {
  40.             c[c_i] = b[b_i];
  41.             b_i += 1;
  42.         }
  43.         else {
  44.             c[c_i] = x[a_i];
  45.             a_i += 1;
  46.         }
  47.     }
  48.     for (int i = 0; i < x_size; ++i) {
  49.         x[i] = c[i];
  50.     }
  51.     delete[] c;
  52. }
  53. void sort_array_bubbl(int* x, int x_size,int& x_d) {
  54.     for (int i = 1; i < x_size; ++i) {
  55.         for (int j = 1; j < x_size; ++j) {
  56.             if (x[j] < x[j - 1]) {
  57.                 swap(x[j], x[j - 1]);
  58.             }
  59.             x_d += 1;
  60.         }
  61.     }
  62. }
  63. int main() {
  64.     int n;
  65.     cin >> n;
  66.     int* a = new int[n];
  67.     int* b = new int[n];
  68.     int c = 0;
  69.     int d = 0;
  70.     read_array(a, n);
  71.     for (int i = 0; i < n; ++i) {
  72.         b[i] = a[i];
  73.     }
  74.     sort_array(a, n, c);
  75.     sort_array_bubbl(b, n, d);
  76.     cout << "a"<< endl;
  77.     write_array(a, n);
  78.     cout << endl;
  79.     cout << "c =="<<c<<endl;
  80.     cout << "b"<< endl;
  81.     write_array(b, n);
  82.     cout << endl;
  83.     cout << "d=="<< d;
  84.     delete[]a;
  85.     delete[]b;
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement