kokokozhina

matrixsort_7

Feb 18th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iomanip>
  6. //В файле содержится двумерный массив размерностью 𝑛 × 𝑛. В новый файл вывести отсортированный массив
  7. using namespace std;
  8. //Четные строки по убыванию, нечетные по возрастанию с помощью сортировки Шелла с шагом 𝑑 = 𝑁 / 2.
  9.  
  10. void shell(int **a, int n, int z) //сортировка Шелла
  11. {
  12.     int d = n / 2;
  13.     while (d > 0)
  14.     {
  15.         for (int i = 0; i < n - d; i++)
  16.         {
  17.             int j = i;
  18.             while (j >= 0 && (a[z][j] > a[z][j + d] && z % 2 || a[z][j] < a[z][j + d] && z % 2 == 0))
  19.             {
  20.                 swap(a[z][j], a[z][j + d]);
  21.                 j--;
  22.             }
  23.         }
  24.         d /= 2;
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     ifstream in("in.txt");
  31.     ofstream out("out.txt");
  32.    
  33.     int n;
  34.     cout << "Enter array's dimension: ";
  35.     cin >> n;
  36.     int** a = new int*[n];
  37.     for(int i = 0; i < n; i++)
  38.     {
  39.         a[i] = new int[n];
  40.         for(int j = 0; j < n; j++)
  41.         {
  42.             in >> a[i][j];
  43.         }
  44.     }
  45.  
  46.     for(int i = 0; i < n; i++)
  47.     {
  48.         shell(a, n, i);
  49.     }
  50.  
  51.     for(int i = 0; i < n; i++, out << endl)
  52.     {
  53.         for(int j = 0; j < n; j++)
  54.         {
  55.             out << a[i][j] << " ";
  56.         }
  57.     }
  58. }
Add Comment
Please, Sign In to add comment