C中的矩阵排序

Matrix sorting in C

我有如下矩阵

Col1 Col2   Col3
5   10      3
2   4       0
7   14      0
2   6       1
1   2       1
4   8       2
6   12      3

我想根据 Col2 按升序对矩阵进行排序。矩阵应该看起来像

Col1 Col2   Col3
1     2      1
2     4      0
2     6      1
4     8      2
5     10     3
6     12     3
7     14     0

我不确定 C 中可用的任何方法是如何执行此操作的。 非常感谢任何帮助

#include <stdio.h>
#include <stdlib.h>

#define COL_SIZE 3

int cmp(const void *a, const void *b){
#if 0
    int (*x)[COL_SIZE] = (int(*)[COL_SIZE])a;
    int (*y)[COL_SIZE] = (int(*)[COL_SIZE])b;
    int col2_0 = x[0][1];
    int col2_1 = y[0][1];
#endif
    int col2_0 = ((int *)a)[1];
    int col2_1 = ((int *)b)[1];
    return (col2_0 > col2_1) - (col2_0 < col2_1);
}

int main(void) {
    int matrix[][COL_SIZE] = {
        {5, 10, 3},
        {2,  4, 0},
        {7, 14, 0},
        {2,  6, 1},
        {1,  2, 1},
        {4,  8, 2},
        {6, 12, 3}
    };
    size_t sizeof_row = sizeof(*matrix);
    size_t num_of_row = sizeof(matrix) / sizeof_row;

    qsort(matrix, num_of_row, sizeof_row, cmp);

    for(int r = 0; r < num_of_row; ++r){
        for(int c = 0; c < COL_SIZE; ++c){
            printf("%2d ", matrix[r][c]);
        }
        puts("");
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

typedef int(*comparer) (int a, int b);

int compareasc ( const void *pa, const void *pb ) {
    const int *a = *(const int **)pa;
    const int *b = *(const int **)pb;
    if(a[0] == b[0])
        return a[1] - b[1];
    else
        return a[0] - b[0];
}
int comparedsc ( const void *pa, const void *pb ) {
    const int *a = *(const int **)pa;
    const int *b = *(const int **)pb;
    if(a[2] == b[2])
        return b[1] - a[1];
    else
        return b[2] - a[2];
}

int main(void){
    int **array,**array2;
  //  int 8 = 10;
    //int i;
    int pid[8],priority[8],arrival[8];
    FILE *fp;
    char buff[255];
    fp = fopen("process.rtf","r");
    if(fp ==NULL)
      perror("File not found");
    else{
        int i =0;
        while(fgets(buff,100,fp)!=NULL){
          sscanf(buff,"%d  %d  %d",&pid[i],&arrival[i],&priority[i]);
          i++;
          }
        fclose(fp);
        }
    for(int i=0;i<8;i++){
      pid[i] = pid[i+1];
      priority[i]=priority[i+1];
      arrival[i] = arrival[i+1];
    }
    /*
    * Sorting the dataset in Ascending order based on the Arrival time
    */
    array = malloc(8 * sizeof(int*));
    for (int i = 0; i < 8; i++){
        array[i] = malloc(2 * sizeof(int));
        array[i][0] = pid[i];
        array[i][1] = arrival[i];
        array[i][2] = priority[i];
    }
    printf("The original dataset\n");
    for(int i = 0;i < 7;++i)
        printf("%2d %2d %2d\n", array[i][0], array[i][1],array[i][2]);

    printf("\n");

    printf("Dataset sorted based on the arrival in ascending order:\n");
    qsort(array, 8, sizeof array[2], compareasc);

    for(int i = 1;i < 8;++i)
        printf("%2d %2d %2d\n", array[i][0], array[i][1],array[i][2]);

      /*--------------------------------------------------*/
      /*
      * Sorting the dataset in Desceding order based on the priority
      */
      printf("\n");
      array2 = malloc(8 * sizeof(int*));
      for (int i = 0; i < 8; i++){
          array2[i] = malloc(2 * sizeof(int));
          array2[i][0] = pid[i];
          array2[i][1] = arrival[i];
          array2[i][2] = priority[i];
      }
      printf("Original Dataset:\n");
      for(int i = 0;i < 7;++i)
          printf("%2d %2d %2d\n", array2[i][0], array2[i][1],array2[i][2]);

      printf("\n");
      printf("Dataset sorted based on priority in descending order:\n");
      qsort(array2, 8, sizeof array2[2], comparedsc);

      for(int i = 1;i < 8;++i)
          printf("%2d %2d %2d\n", array2[i][0], array2[i][1],array2[i][2]);

    return 0;
}