|
查看: 1379|回复: 5
|
请问下?? 如何SORT multidimensional array
[复制链接]
|
|
|
google 了许多 网站 看到 都 要昏了
JAVA 例子 C++ 也好 只要知道 它的 FLOW
single 的就 可以了解 到了 multidimensional 就不行了
请高手指点指点下。 |
|
|
|
|
|
|
|
|
|
|
发表于 6-5-2009 07:49 PM
|
显示全部楼层
请问multidimensional array的sort是什么意思?
你怎么分大小??
单纯的一个array,
int a[4] = {4,3,2,1};
sort后是{1,2,3,4}
1<2<3<4
但是multidimensional array呢? |
|
|
|
|
|
|
|
|
|
|
发表于 7-5-2009 09:57 PM
|
显示全部楼层
可以給個example嗎?
不然不是很明白你要什麼 |
|
|
|
|
|
|
|
|
|
|
发表于 15-5-2009 05:58 PM
|
显示全部楼层
蛮有趣下。。。我从来都没有试过sort multidimensional array呢
{ 1 , 2 , 3 , 4 }
{ 1 , 2 , 3 , 4 }
嗯。。。这样算是sorted的multidimensional array吗?我觉得如果sorting MD array, the value of the current array index should be larger or equal to it's previous and top one.
Let say
array[2][4] = {
{ 1 , 2 , 3 , 4 },
{ 1 , 4 , 2 , 3 }
}
So, at array[2][2], it's at least equal or larger than array[2 - 1][2] and array[2][2 - 1], so is this consider as sorted? But, at array[2][3], it is smaller than array[2][2], so it should perform a swap. But what if after it swapped, it is smaller than array[1][2] (which means the top one). It's so complicated, sorry if something wrong with my algorithm.
But, if you are saying that, you only sort a multidemensional array based on 1 key, then it would be easier.
Such as, an array which contain Rank, Name. Then you only wanted to sort base on Rank. Then you can just sort it normally (with which ever sorting algorithm you want, such as insertion, selection or quicksort etc), and swap the data at the same time.
Or if you want things to be first sorted with Rank, and next sorted by the alphabetical order of Name. Then the only scenario where this would happen is, there exist more than 1 name with the same rank. But still, it would be easy, because you are still doing a linear swap, just that with a different condition. |
|
|
|
|
|
|
|
|
|
|
发表于 16-5-2009 01:22 AM
|
显示全部楼层
何为sort multidimentional array ?
{4,1,3,2 }
{4,1,3,2 }
sort 后为
{1,2,3,4}
{1,2,3,4} ?
还是
{1,1,2,2,}
{3,3,4,4}
你要得哪一种? |
|
|
|
|
|
|
|
|
|
|
发表于 30-5-2009 12:07 AM
|
显示全部楼层
如果要sort column or row就用sort single array的方法就行了
example:
{8,7,6,5}
{4,3,2,1}
for (i=0; i<array.length; i++)
for (j=0; j<array.length; j++)
......sort........(懒惰打,自己想)
如果要sort成酱的话
{1,2,3,4}
{5,6,7,8}
create new array
int[] a = new int[?]
copy to array a
sort
copy back
不过我不建议酱紫做,我觉得直接用single array更方便 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|