AddO1f

Algor2_2

Apr 1st, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     int k;
  10.     int op=0;
  11.     int mas[100];
  12.     cout << "razmer:";
  13.     cin >> n;
  14.     cout << "massiv:";
  15.     for (int i = 0; i < n; i++)
  16.     {
  17.         cin >> mas[i];
  18.     }
  19.     for (k = n / 2;k > 0;k /= 2)
  20.     {      
  21.         for (int i =n-1; i >= 0; i--)
  22.         {
  23.             int t = i;
  24.             while (t-k>=0)
  25.             {
  26.                 if (mas[t] < mas[t - k]) { swap(mas[t], mas[t - k]); op++; }
  27.                 t -= k;
  28.             }
  29.         }
  30.     }
  31.     if (mas[n-1] < mas[n-2]) { swap(mas[n - 1], mas[n - 2]); op++;}
  32.     cout << "sort:";
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         cout << mas[i]<<" ";
  36.     }
  37.     cout << endl;
  38.     cout << "operazii:"<< op << endl;
  39.     system("pause");
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment