【问题标题】:Matrix sorting in CC中的矩阵排序
【发布时间】:2016-01-26 08:29:15
【问题描述】:

我有一个像下面这样的矩阵

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 中可用的任何方法如何执行此操作。 非常感谢任何帮助

【问题讨论】:

  • 读入文件的每一行,解析行提取(至少)Col2值,将数据记录成以Col2值为key的数据结构,排序输出。这就是通用算法。尝试一次编码每个步骤。如果您有任何具体困难,请展示您的代码并提出具体问题。
  • 您可以创建一个由 3 个整数组成的结构,每个整数代表每一列,用您的数据填充该结构,然后运行一些循环对数据进行排序,然后将结果打印到屏幕上。从网上的教程开始,然后分享您迄今为止尝试过的代码,以便我们提供更好的帮助。
  • 你是问做它的逻辑还是代码本身?
  • 我找到了解决问题的方法。我已经使用 qsort 方法进行排序。我使用比较器来比较我的值并按行排序。

标签: c sorting matrix columnsorting


【解决方案1】:
#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;
}

【讨论】:

    【解决方案2】:
    #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;
    }
    

    【讨论】:

    • 存在 arrayIndexOutOfBounds 问题:在索引 8 处访问的数组 'priority[8]' 超出范围 for(int i=0;i
    猜你喜欢
    • 2022-12-22
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多