[英]How to efficiently sort a Multidimensional Arrray
I was given a task to sort multidimensional array into ascending order without using the pre-made functions in the Array class (such as .sort). 我得到的任务是在不使用Array类中的预制函数(例如.sort)的情况下将多维数组按升序排序。
I've tried asking some of my friends for ideas... Many of them turns the array into a single-dimensional array, sort it the way you would sort a single-dimensional array and then turns it back into a multidimensional array. 我曾尝试向我的一些朋友征求意见……许多人将数组转换为一维数组,以对一维数组进行排序的方式对其进行排序,然后将其转换回多维数组。
I'm just curious to know if there'd be any other ways to do this without having to go through such trouble. 我只是想知道是否还有其他方法可以不必经历这种麻烦。
here is a full exemple you can tri 这是一个完整的例子,你可以三
import java.util.Arrays;
import java.util.Comparator;
public class PartNumberQuantityDetailer {
// initialize a two dimensional array
static Integer[][] itemIdAndQty = new Integer[5][2];
public static void main(String[] args) {
// initialize array values
itemIdAndQty[0][0] = 1234;
itemIdAndQty[0][1] = 46;
itemIdAndQty[1][0] = 5443;
itemIdAndQty[1][1] = 564;
itemIdAndQty[2][0] = 362;
itemIdAndQty[2][1] = 24;
itemIdAndQty[3][0] = 6742;
itemIdAndQty[3][1] = 825;
itemIdAndQty[4][0] = 347;
itemIdAndQty[4][1] = 549;
System.out.println("Before sorting");
// show the contents of array
displayArray();
// sort the array on item id(first column)
Arrays.sort(itemIdAndQty, new Comparator<Integer[]>() {
@Override
//arguments to this method represent the arrays to be sorted
public int compare(Integer[] o1, Integer[] o2) {
//get the item ids which are at index 0 of the array
Integer itemIdOne = o1[0];
Integer itemIdTwo = o2[0];
// sort on item id
return itemIdOne.compareTo(itemIdTwo);
}
});
// display array after sort
System.out.println("After sorting on item id in ascending order");
displayArray();
// sort array on quantity(second column)
Arrays.sort(itemIdAndQty, new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
Integer quantityOne = o1[1];
Integer quantityTwo = o2[1];
// reverse sort on quantity
return quantityOne.compareTo(quantityTwo);
}
});
// display array after sort
System.out.println("After sorting on quantity in ascending order");
displayArray();
}
private static void displayArray() {
System.out.println("-------------------------------------");
System.out.println("Item id\t\tQuantity");
for (int i = 0; i < itemIdAndQty.length; i++) {
Integer[] itemRecord = itemIdAndQty[i];
System.out.println(itemRecord[0] + "\t\t" + itemRecord[1]);
}
System.out.println("-------------------------------------");
}
}] }]
sort multidimensional array into ascending order: 将多维数组升序排列:
You can sort multi-d array row-wise or column -wise. 您可以按行或列对多维数组进行排序。
If you don't want to flatten the array that is convert it into 1-d then that mean you have to go through each row or column depending on your choice and apply quick-sort(better performance for small data set if pivot is chosen optimally) or merge-sort. 如果您不想展平将其转换为1-d的数组,那么这意味着您必须根据自己的选择遍历每一行或每一列,并应用快速排序(如果选择了数据透视,则对小型数据集的性能更好最佳)或合并排序。
So you can do both in (considering avg. case) O(nlogn)
and say there are n rown or n columns the time complexity will be O(n^2(logn))
. 因此,您可以在(考虑到平均情况)
O(nlogn)
并说存在n个rown或n列,时间复杂度将为O(n^2(logn))
。
Now above the assumption is that you want to sort either row-wise or column-wise. 现在,以上假设是您要按行或按列排序。 If you want to achieve the both(row and column) then it's better if you convert the array to 1-d and then apply sort and then convert it back.
如果要同时实现(行和列),最好将数组转换为1-d,然后应用sort然后再转换回。 Otherwise following above approach the time complexity can go
O(n^2(logn))
but in the other case it will be O((n+m)log(n+m))
where n
amd m
are no. 否则,按照上述方法,时间复杂度可以变为
O(n^2(logn))
但在另一种情况下,它将是O((n+m)log(n+m))
,其中n
amd m
为no。 of rows and columns in array plus the O(n+m)
space complexity. 数组中的行和列数加上
O(n+m)
空间复杂度。
In my opinion having a bit of space complexity so that you can bring down the run-time is preferable. 在我看来,具有一点空间复杂性以便您可以缩短运行时间是可取的。
I've found a solution... Thanks all 我找到了解决方法...谢谢大家
public static void sortAscending(int array[][]) { 公共静态无效sortAscending(int array [] []){
int temp = 0;
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++) {
for(int k = 0; k < array.length; k++) {
for(int l = 0; l < array[k].length; l++) {
if(array[i][j] < array[k][l]) {
temp = array[i][j];
array[i][j] = array[k][l];
array[k][l] = temp;
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.