Advertisement
titytus

Sort_Matrix_2c

Oct 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. void Xuat(int A[][4], int row, int col);
  3. void Sort(int A[][4], int row, int col);
  4. void swap(int A[][4], int iR, int iC, int jR, int jC);
  5.  
  6. int main()
  7. {
  8.     int A[4][4]=
  9.     {
  10.         {7,4,0,4},
  11.         {3,8,1,5},
  12.         {9,2,6,3},
  13.         {1,0,3,7}
  14.     };
  15.     int row=4;
  16.     int col=4;
  17.     //Xuat(A,row,col);
  18.     Sort(A,row,col);
  19.     Xuat(A,row,col);
  20.     return 0;
  21. }
  22.  
  23. void Xuat(int A[][4], int row, int col)
  24. {
  25.     for (int i=0;i<row;i++)
  26.     {
  27.         for (int j=0;j<col;j++)
  28.             printf("%d ",A[i][j]);
  29.         printf("\n");
  30.     }
  31.        
  32. }
  33.  
  34. void Sort(int A[][4], int row, int col)
  35. {
  36.     for (int i=0;i<row*col-1;i++)
  37.         for (int j=i+1;j<row*col;j++)
  38.         {
  39.             if (A[i/col][i%col]>A[j/col][j%col])
  40.                 swap(A,i/col,i%col,j/col,j%col);
  41.         }
  42.     for (int i=1;i<row;i+=2)
  43.         for (int j=0;j<col/2;j++)
  44.             swap(A,i,j,i,col-j-1);
  45. }
  46.  
  47. void swap(int A[][4], int iR, int iC, int jR, int jC)
  48. {
  49.     int tmp=A[iR][iC];
  50.     A[iR][iC]=A[jR][jC];
  51.     A[jR][jC]=tmp;
  52. }
  53.  
  54. /*
  55. 00 01 02 03 04 05 06 07 08 09
  56. 10 11 12 13 14 15 16 17 18 19
  57. 20 21 22 23 24 25 26 27 28 29
  58.  
  59. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement