繁体   English   中英

使用qsort对数组进行排序的结构

[英]Sort structure of arrays using qsort

我有一个包含两个数组的结构:矩阵的行索引和列索引。 这些索引不是顺序的,我想使用qsort对其进行排序。

我不想使用的东西

我知道,如果我有一系列结构,这很容易。 然后可以如下所示

// structure to store the row/column index
typedef struct Index {
  int row;
  int col;
} Index;

// function to compare two entries

int cmp(const void *a, const void *b){

    Index *Ia = (Index *) a;
    Index *Ib = (Index *) b;

    if(Ia->row  < Ib->row                      ) return -1;
    if(Ia->row == Ib->row && Ia->col  < Ib->col) return -1;
    if(Ia->row == Ib->row && Ia->col == Ib->col) return  0;
    if(Ia->row == Ib->row && Ia->col  > Ib->col) return  1;
    if(Ia->row  > Ib->row                      ) return  1;

}

// main program
int main(void) {

  int N = 3;
  Index mat[N];

  // fill the matrix with fictitious data
  mat[0].row = 1;   mat[0].col = 3;
  mat[1].row = 0;   mat[0].col = 2;
  mat[2].row = 0;   mat[0].col = 1;

  // sort the "matrix": first ascending rows, then ascending columns
  qsort(mat,N,sizeof(Index),cmp);

  return 0;

}

我想用什么

我的程序构造为没有结构数组,但是有数组结构

// define structure
typedef struct Matrix {
  int* row; 
  int* col; 
} Matrix;

// main program
int main(void) {

  // define fictitious data
  int row[3] = { 1 , 1 , 0 };
  int col[3] = { 3 , 2 , 1 };

  // define matrix
  Sparse mat;
  mat.row = row;
  mat.col = col;

  // sort
  // ...?


  return 0;

}

我想对上面的行/列索引进行排序。 到目前为止,我已将数据复制到一个结构数组中,进行了排序并复制回去。 但是,我正在使用的数据集太大,因此我想避免这种情况。

谢谢!

不是答案,而是关于您在qsort()中使用compare()函数的方式的评论。 这样会更有效。

int cmp(const void *a, const void *b){
    Index *Ia = (Index *) a;
    Index *Ib = (Index *) b;

    if(Ia->row < Ib->row) return -1;
    if(Ia->row > Ib->row) return  1;
    if(Ia->col < Ib->col) return -1;
    if(Ia->col > Ib->col) return  1;
    return  0;
}

如果您不想使用额外的内存,则应按以下方式编写qsort(我仅作了简短测试):qsort的原始来源: http//zh.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort

#include <stdio.h>

typedef struct Matrix {
  int* row; 
  int* col; 
} Matrix;

int cmp(Matrix* m, int a, int b)
{
    if( m->row[a] != m->row[b] )
        return m->row[a] - m->row[b];
    return m->col[a] - m->col[b];
}

void swap(Matrix *m, int l, int r) 
{
   int tmp;
   tmp = m->row[l];
   m->row[l] = m->row[r];
   m->row[r] = tmp;

   tmp = m->col[l];
   m->col[l] = m->col[r];
   m->col[r] = tmp;
}

void sort(Matrix* tab, int begin, int end) 
{
   if (end > begin) {
      int pivot = begin;
      int l = begin;
      int r = end;
      while(l < r) {
         if (cmp(tab, l, pivot) <= 0) {
            ++l;
         } else if ( cmp(tab, r, pivot) > 0 )  {
            --r;
         } else if ( l < r ) {
            swap(tab, l, r);
         }
      }
      --l;
      swap(tab, begin, l);
      sort(tab, begin, l);
      sort(tab, r, end);
   }
}

int main(void) {

  const int N = 3;

  Matrix matrix;
  int row[N] = { 1 , 1 , 0 };
  int col[N] = { 3 , 2 , 1 };

  matrix.row = row;
  matrix.col = col;

  sort( &matrix, 0, N );

  for(int i = 0; i < N; ++i)
      printf("%d %d\n", matrix.row[i], matrix.col[i]);

  return 0;
}
#include <stdio.h>
#include <stdlib.h>

typedef struct Matrix {
  int* row; 
  int* col; 
} Matrix;

Matrix *mat_cmp;//see from the comparison function

int cmp(const void *a, const void *b){
    int ia = *(int *)a;
    int ib = *(int *)b;
    int ra = mat_cmp->row[ia];
    int rb = mat_cmp->row[ib];
    if(ra == rb){
        int ca = mat_cmp->col[ia];
        int cb = mat_cmp->col[ib];
        return (ca > cb) - (ca < cb);
    } else
        return (ra > rb) - (ra < rb);
}

#define N 3

int main(void) {
    int row[N] = { 1 , 1 , 0 };
    int col[N] = { 3 , 2 , 1 };
    int i, index[N];
    Matrix mat = { .row=row, .col=col};

    for(i=0; i<N; ++i)
        index[i] = i;//set index(0..N-1)
    mat_cmp = &mat;
    qsort(index, N, sizeof(*index), cmp);
    for(i=0;i<N;i++){
        printf("%d,%d\n", mat.row[index[i]], mat.col[index[i]]);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM