使用qsort对2d数组进行排序 [英] sorting 2d arrays using qsort

查看:70
本文介绍了使用qsort对2d数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai

这是我的二维阵列。

int a [] [6] = {

{5,2,20, 1,30,10},

{23,15,7,9,11,3},

{40,50,34,24,14,4} ,

{9,10,11,12,13,14},

{31,4,18,8,27,17},

{44,32,13,19,41,19},

{1,2,3,4,5,6},

{80, 37,47,18,21,9}

};

之后

for(i = 0; i< box; i ++)

qsort(a [i],dim,sizeof(int),dim * _sort);

int dim_sort(const void * a,const void * b)

{

return(*(int *)a - *(int *)b);

}

其中昏暗= 6,我得到了以下信息:

1 2 5 10 20 30

3 7 9 11 15 23

4 14 24 34 40 50

9 10 11 12 13 14

4 8 17 18 27 31

13 19 19 32 41 44

1 2 3 4 5 6

9 18 21 37 47 80

(工作正常!)

现在我想对每个人进行排序上面数组的元素行,

喜欢

1 2 3 4 5 6

1 2 5 10 20 30

....




什么是qsort例程和功能,有人可以帮助我吗?

欢呼,

badri

hai
this is my 2d array.
int a[][6] = {
{5,2,20,1,30,10},
{23,15,7,9,11,3},
{40,50,34,24,14,4},
{9,10,11,12,13,14},
{31,4,18,8,27,17},
{44,32,13,19,41,19},
{1,2,3,4,5,6},
{80,37,47,18,21,9}
};
after
for(i=0;i<box;i++)
qsort(a[i],dim,sizeof(int),dim*_sort);
int dim_sort(const void *a,const void *b)
{
return ( *(int*)a - *(int*)b);
}
where dim=6, i get the foll o/p:
1 2 5 10 20 30
3 7 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
1 2 3 4 5 6
9 18 21 37 47 80
(works fine!)
now i want to sort each row of elements for above array,
like
1 2 3 4 5 6
1 2 5 10 20 30
....
etc.

what is the qsort routine and function, can anybody help me ?
cheers,
badri

推荐答案

> return(*(int *)a - *(int *)b);

这是比较两个有符号整数的危险方法。你有冒险

下溢,这是未定义的。
> return ( *(int*)a - *(int*)b);
That''s a dangerous way to compare two signed integers. You''re risking
underflow, which is undefined.
现在我想对上面数组的每一行元素进行排序
now i want to sort each row of elements for above array



你不能用qsort做,因为qsort试图直接交换两个

项;数组不支持的操作。但是,您可以创建一个指向int的指针数组,将它们指向a中的每个数组,然后通过编写另一个比较函数来对该数组进行排序。 br />
被比较的两个阵列中的第一个不匹配:

#include< stdio.h>

#include< stdlib .h>


static int arraya [] [6] = {

{5,2,20,1,30,10},
{23,15,7,9,11,3},

{40,50,34,24,14,4},

{ 9,10,11,12,13,14},

{31,4,18,8,27,17},

{44,32,13, 19,41,19},

{1,2,3,4,5,6},

{80,37,47,18,21,9}

};


static int * arrayb [8];


int cmp_int(const void * a, const void * b)

{

const int * ia = a;

const int * ib = b;


如果(* ia< * ib)返回-1;

否则if(* ia> * ib)返回+1;

else返回0;

}


int cmp_array(const void * a,const void * b)

{

const int ** ia = a;

const int ** ib = b;

int i,cmp;


for(i = 0;我< 6; i ++){

if((cmp = cmp_int(&(* ia)[i],&(* ib)[i]))!= 0)

返回cmp;

}


返回0;

}


int main(无效)

{

int i,j;


for(i = 0; i< 8; i ++ )

qsort(arraya [i],6,sizeof(int),cmp_int);


for(i = 0; i< 8; i ++ ){

for(j = 0; j< 6; j ++)

printf("%4d",arraya [i] [j]);

printf(" \ n");

}


printf(" \ n");


for(i = 0; i< 8; i ++)

arrayb [i] = arraya [i];


qsort(arrayb,8,sizeof(int *),cmp_array);


for(i = 0; i< 8; i ++){

for(j = 0; j< 6; j ++)

printf("%4d",arrayb [i] [j]);

printf(" ; \ n");

}


printf(" \ n");

}


200年6月11日星期六5 05:35:06 -0700,yatindran写道:
On Sat, 11 Jun 2005 05:35:06 -0700, yatindran wrote:
hai
这是我的2D阵列。
int a [] [6] = {
{5,2,20,1,30,10},
{23,15,7,9,11,3},
{40,50,34,24,14,4 },
{9,10,11,12,13,14},
{31,4,18,8,27,17},
{44,32,13,19 ,41,19},
{1,2,3,4,5,6},
{80,37,47,18,21,9}
};

之后(i = 0; i< box; i ++)
qsort(a [i],dim,sizeof(int),dim * _sort);


你可以把它写成

int dim_sort(const void * a,const void * b)
{
返回(* (int *)a - *(int *)b);

}


小心,减去2个整数会溢出所以这不好定义

,除非你能确定

数组中任何2个值之间的差异可以表示为int。这样做会更安全:

这样的假设:


int dim_sort(const void * va,const void * vb)

{

int a = *(const int *)va;

int b = *(const int *)vb;


返回(a< b)? -1:(a> b);

}

其中dim = 6,我得到了foll o / p:
1 2 5 10 20 30 < br 3> 3 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
1 2 3 4 5 6
9 18 21 37 47 80
(工作正常!)


好​​的,你正在分拣每个内容单独排,这很好。

现在我想对上面数组的每一行元素进行排序,
1/3 4 5 6
1 2 5 10 20 30




什么是qsort例程和功能,有人可以帮助我吗?
hai
this is my 2d array.
int a[][6] = {
{5,2,20,1,30,10},
{23,15,7,9,11,3},
{40,50,34,24,14,4},
{9,10,11,12,13,14},
{31,4,18,8,27,17},
{44,32,13,19,41,19},
{1,2,3,4,5,6},
{80,37,47,18,21,9}
};
after
for(i=0;i<box;i++)
qsort(a[i],dim,sizeof(int),dim*_sort);
You could write this as
int dim_sort(const void *a,const void *b)
{
return ( *(int*)a - *(int*)b);
}
Be caseful, subtracting 2 ints can overflow so this isn''t well defined
unless you can be sure that the difference between any 2 values in the
array is representable as an int. It would be safer not to make
assumptions like this:

int dim_sort(const void *va, const void *vb)
{
int a = *(const int *)va;
int b = *(const int *)vb;

return (a<b) ? -1 : (a>b);
}
where dim=6, i get the foll o/p:
1 2 5 10 20 30
3 7 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
1 2 3 4 5 6
9 18 21 37 47 80
(works fine!)
OK, you''re sorting the contents each row individually which is fine.
now i want to sort each row of elements for above array,
like
1 2 3 4 5 6
1 2 5 10 20 30
...
etc.

what is the qsort routine and function, can anybody help me ?




你只需要记住,数组a只是一个元素数组

其中每个元素本身就是一个数组,所以你可以写:


qsort(a,sizeof a / sizeof * a,sizeof * a,compare_rows);


其中sizeof a / sizeof * a是a中的行数,sizeof * a是一行的字节数

。比较函数类似于


static int compare_rows(const void * va,const void * vb)

{

const int * pa = *(const int(*)[6])va;

const int * pb = *(const int(*)[6])vb;

int i;


for(i = 0; i< 6; i ++){

if(pa [i]!= pb [i ])

返回(pa [i]> pb [i])? 1:-1;

}


返回0;

}


Lawrence



You just have to remember that the array a is simply an array of elements
where each of those elements is itself an array, so you can write:

qsort(a, sizeof a/sizeof *a, sizeof *a, compare_rows);

Where sizeof a/sizeof *a is the number of rows in a, sizeof *a is the
size in bytes of a row. The comparison function would be something like

static int compare_rows(const void *va, const void *vb)
{
const int *pa = *(const int (*)[6])va;
const int *pb = *(const int (*)[6])vb;
int i;

for (i = 0; i < 6; i++) {
if (pa[i] != pb[i])
return (pa[i] > pb[i]) ? 1 : -1;
}

return 0;
}

Lawrence


2005年6月11日星期六07:27:29 -0700,James Daughtry写道:
On Sat, 11 Jun 2005 07:27:29 -0700, James Daughtry wrote:
return(*(int *)a - *(int *)b);这是比较两个有符号整数的危险方法。你冒着下溢的风险,这是未定义的。
return ( *(int*)a - *(int*)b); That''s a dangerous way to compare two signed integers. You''re risking
underflow, which is undefined.
现在我想对上面数组的每一行元素进行排序
now i want to sort each row of elements for above array


你可以'用qsort做,因为qsort试图直接交换两个
项目;数组不支持的操作。


You can''t do it with qsort because qsort tries to directly exchange two
items; an operation that arrays don''t support.




C不直接为任何类型提供交换操作,数组是

在这方面没有什么不同。事实上,qsort()的定义中没有任何内容要求它使用交换,尽管许多常见的
排序算法确实使用它们。更基本的操作是副本。

qsort()不知道或不关心数组元素的类型

它正在排序,特别是它并不关心它们是否是

本身是否为数组。在C中你可以复制(并因此交换一对

对)任何对象,将其视为sizeof(object)字节数组

(特别是unsigned char数组) )。 qsort()的实现是

可能使用memcpy()或等效的复制工具

对数组很好。它需要做的就是指向对象的指针和对象的大小(以字节为单位)。所有必要的信息都在
qsort()的参数中传递。

但是,您可以创建一个指向int的指针数组,指向它们a中的每个数组,然后通过编写另一个比较函数来对该数组进行排序,该比较函数比较两个被比较数组中的第一个不匹配:



C doesn''t directly supply an exchange operation for any type, arrays are
no different in this respect. In fact there is nothing in the definition
of qsort() that requires it to use exchanges as such, although many common
sorting algorithms do use them. The more fundamental operation is a copy.
qsort() doesn''t know or care about the type of the elements of the array
it is sorting, in particular it doesn''t care whether they are are
themselves arrays or not. In C you can copy (and therefore exchange a
pair of) ANY object by treating it as an array of sizeof(object) bytes
(specifically an array of unsigned char). An implementation of qsort() is
likely to use memcpy() or something equivalent for copying which works
fine on arrays. All it needs to do this is a pointer to the object and the
size of the object in bytes. All the necessary information is passed in
qsort()''s arguments.
You can, however, create
an array of pointers to int, point them to each array in a, and then
sort that array by writing another comparison function that compares
the first mismatch in the two arrays being compared:




你可以做到这一点,并且这样做可能有效率优势(即复制指针的
可能比复制一行更快),但是你

不必。


劳伦斯



You can do that, and there may be efficiency advantages to doing so (i.e.
copying a pointer is likely to be quicker than copying a row), but you
don''t have to.

Lawrence


这篇关于使用qsort对2d数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆