Advertisement
kokokozhina

matrixsort_3

Feb 18th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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. //Столбцы по убыванию помощью гномьей сортировки
  9.  
  10. void gnomeSort(int **a, int n, int j)
  11. {
  12.     int i = 0;
  13.     while(i < n)
  14.     {
  15.         if(i == 0 || a[i - 1][j] >= a[i][j])
  16.             ++i;
  17.         else
  18.         {
  19.             swap(a[i - 1][j], a[i][j]);
  20.             --i;
  21.         }
  22.     }
  23. }
  24.  
  25. int main()
  26. {
  27.     ifstream in("in.txt");
  28.     ofstream out("out.txt");
  29.    
  30.     int n;
  31.     cout << "Enter array's dimension: ";
  32.     cin >> n;
  33.     int** a = new int*[n];
  34.     for(int i = 0; i < n; i++)
  35.     {
  36.         a[i] = new int[n];
  37.         for(int j = 0; j < n; j++)
  38.         {
  39.             in >> a[i][j];
  40.         }
  41.     }
  42.  
  43.     for(int j = 0; j < n; j++)
  44.     {
  45.         gnomeSort(a, n, j);
  46.     }
  47.  
  48.     for(int i = 0; i < n; i++, out << endl)
  49.     {
  50.         for(int j = 0; j < n; j++)
  51.         {
  52.             out << a[i][j] << " ";
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement