Aleksandr_Grigoryev

12.12.2017

Dec 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void sort(int arr[], int N)
  5. {
  6.     for (int i = 1; i < N; i++)
  7.     {
  8.         for (int j = 0; j < i; j++)
  9.         {
  10.             if (arr[j] > arr[i])
  11.                 swap(arr[i], arr[j]);
  12.         }
  13.     }
  14. }
  15.  
  16. int main()
  17. {
  18.  
  19.     int N;
  20.     cout << "N=" << endl;
  21.     cin >> N;
  22.     int* A = new int[N];
  23.  
  24.     for (int i = 0; i < N; i++)
  25.     {
  26.     A[i]=rand()%5;
  27.     cout << A[i] << " ";
  28.     }
  29.  
  30.     int* B = new int[N / 2];
  31.     int* C = new int[N - (N / 2)];
  32.  
  33.     for (int i = 0; i < N; i++)
  34.     {
  35.  
  36.         while (i < N / 2) {
  37.             B[i] = A[i];
  38.             i++;
  39.         }
  40.  
  41.         C[i - (N / 2)] = A[i];
  42.     }
  43.  
  44.     sort(B, N / 2);
  45.     sort(C, N - (N / 2));
  46.  
  47.     int i = 0, j = 0;
  48.  
  49.     for (int f = 0; f < N; f++) {
  50.  
  51.         if ((i < N / 2) && (j < N - (N / 2)))
  52.         {
  53.  
  54.             if (B[i] < C[j])
  55.             {
  56.                 A[f] = B[i];
  57.                 i++;
  58.             }
  59.  
  60.             else
  61.             {
  62.                 A[f] = C[j];
  63.                 j++;
  64.             }
  65.         }
  66.  
  67.         else {
  68.             if (i < N / 2) {
  69.                 A[f] = B[i];
  70.                 i++;
  71.             }
  72.             else {
  73.                 A[f] = C[j];
  74.                 j++;
  75.             }
  76.         }
  77.     }
  78.  
  79.     cout << endl << "MASS";
  80.         for (int i = 0; i < N; i++)
  81.         cout << A[i] << " ";
  82.  
  83.     cout << endl << endl;
  84.     system("pause");
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment