Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int row = 4, col = 4, sorted[16], mtr [4][4] = {
  8. {5,12,9,3},
  9. {6,2,11,7},
  10. {4,8,15,10},
  11. {1,13,16,14}
  12. };
  13.  
  14. for(int r = 0; r < row; r++){
  15. for(int c = 0; c < col ; c++){
  16. cout<<mtr[r][c]<<" ";
  17. }
  18. cout<<endl;
  19. }
  20.  
  21. //Sort matrix in row form
  22. int i = 0;
  23. for(int r = 0; r < row; r++){
  24. for(int c = 0; c < col ; c++){
  25. sorted[i] = mtr[r][c];
  26. i++;
  27. }
  28. }
  29.  
  30. // Apply Insertion sort
  31. for(int x = 1; x < 16; x++)
  32. for(int k = x; k > 0 && sorted[k- 1] > sorted[k]; k--)
  33. swap(sorted[k- 1], sorted[k]);
  34.  
  35. // Print Row ordered matrix
  36. for(int r = 0,i=0; r < row; r++){
  37. for(int c = 0; c < col ; c++){
  38. mtr[r][c] = sorted[i];
  39. i++;
  40. }
  41. }
  42. for(int r = 0; r < row; r++){
  43. for(int c = 0; c < col ; c++){
  44. cout<<mtr[r][c]<<" ";
  45. }
  46. cout<<endl;
  47. }
  48.  
  49. // Print Column ordered matrix
  50. for(int r = 0,i=0; r < row; r++){
  51. for(int c = 0; c < col ; c++){
  52. mtr[c][r] = sorted[i];
  53. i++;
  54. }
  55. }
  56. for(int r = 0; r < row; r++){
  57. for(int c = 0; c < col ; c++){
  58. cout<<mtr[r][c]<<" ";
  59. }
  60. cout<<endl;
  61. }
  62.  
  63.  
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment