fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // расскомментируйте эту строчку и thread_local в следующей,
  5. // если, вдруг, ваш компилятор поддерживает этот заголовок :)
  6. // #include <threads.h>
  7. /* thread_local */ int column_;
  8.  
  9. int comparator(const void *element1, const void *element2) {
  10. int *line1 = (int *) element1;
  11. int *line2 = (int *) element2;
  12. for (int j = 0; j < column_; ++j) {
  13. if (line1[j] != line2[j]) {
  14. return line2[j] - line1[j];
  15. }
  16. }
  17. return 0;
  18. }
  19.  
  20. void sortMatrix(int **array, int row, int column) {
  21. column_ = column;
  22. qsort(array, (size_t) row, sizeof(int *), comparator);
  23. }
  24.  
  25. int main() {
  26. int row = 4;
  27. int column = 4;
  28.  
  29. // этот массив нужен только для удобства инициализации основного массива
  30. int arrayOnStack[4][4] = {
  31. {2, 3, 4, 5},
  32. {2, 1, 5, 6},
  33. {2, 1, 3, 4},
  34. {1, 2, 3, 4}
  35. };
  36.  
  37. int **array = (int **) malloc(sizeof(int *) * row);
  38. for (int i = 0; i < row; ++i) {
  39. array[i] = (int *) malloc(sizeof(int) * column);
  40. for (int j = 0; j < column; ++j) {
  41. array[i][j] = arrayOnStack[i][j];
  42. }
  43. }
  44.  
  45. printf("До сортировки:\n");
  46. for (int i = 0; i < row; ++i) {
  47. for (int j = 0; j < column; ++j) {
  48. printf("%d%c", array[i][j], " \n"[j == column - 1]);
  49. }
  50. }
  51.  
  52. sortMatrix((int **) array, row, column);
  53.  
  54. printf("\n");
  55. printf("После сортировки:\n");
  56. for (int i = 0; i < row; ++i) {
  57. for (int j = 0; j < column; ++j) {
  58. printf("%d%c", array[i][j], " \n"[j == column - 1]);
  59. }
  60. }
  61. return 0;
  62. }
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
До сортировки:
2 3 4 5
2 1 5 6
2 1 3 4
1 2 3 4

После сортировки:
1 2 3 4
2 1 3 4
2 1 5 6
2 3 4 5