簡體   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