Advertisement
alem47

Двовимірний массив(сортування)

Jul 8th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include<chrono>
  3.  
  4. int main()
  5. {
  6.     srand(time(0));
  7.     const int ROW = 3;
  8.     const int COLL = 5;
  9.     int arr[ROW][COLL]{};
  10.     for (int i = 0; i < ROW; i++)
  11.     {
  12.         for (int j = 0; j < COLL; j++)
  13.         {
  14.             arr[i][j] = rand() % 30 + 1;
  15.             std::cout << arr[i][j] << "  ";
  16.         }
  17.     }
  18.     std::cout << "\n\nAfter sorting\n\n";
  19.  
  20.     bool sort = false;
  21.     while (!sort)
  22.     {
  23.         sort = true;
  24.         int i = 0, j = 0;
  25.         for (int inext = 0; inext < ROW; inext++)
  26.         {
  27.             for (int jnext = (inext ? 0 : 1); jnext < COLL; jnext++)
  28.             {
  29.                 if (arr[i][j] > arr[inext][jnext])
  30.                 {
  31.                     int tmp = arr[i][j];
  32.                     arr[i][j] = arr[inext][jnext];
  33.                     arr[inext][jnext] = tmp;
  34.                     sort = false;
  35.                 }
  36.                 i = inext;
  37.                 j = jnext;
  38.  
  39.             }
  40.         }
  41.     }
  42.     for (int i = 0; i < ROW; i++)
  43.     {
  44.         for (int j = 0; j < COLL; j++)
  45.         {
  46.             std::cout << arr[i][j] << "  ";
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement