我有一个包含两个数组的结构:矩阵的行索引和列索引。这些索引不是订单,我想使用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;
}
我想对行/列索引进行排序,如上所述。到目前为止,我将数据复制到结构数组中,排序并复制回来。但是,我使用的数据集太大了,我想避免这种情况。
谢谢!
答案 0 :(得分:0)
不是答案,而是评论compare()
中使用qsort()
功能的方式。这样会更有效率。
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;
}
答案 1 :(得分:0)
如果您不想使用额外的内存,那么您应该像下面那样编写qsort(我只进行了短测试): qsort的原始来源:http://en.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;
}
答案 2 :(得分:-1)
#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;
}