列排序的依据是什么呢?
追问升序降序差别很大吗?
应该是一样的
追答我说的依据是,以什么为关键字对列进行排序?是每列的行首元素、最大元素、元素之和还是方差,还是其他什么依据?
追问每列的行首元素,亲
追答import java.util.Arrays;
public class Test {
public static<T extends Comparable<? super T>> void sort(T a[][]){
sort(a, true);
}
public static<T extends Comparable<? super T>> void sort(T a[][], boolean ascend){
sortHelper(a, 0, a.length - 1, ascend);
}
private static<T extends Comparable<? super T>> void sortHelper(T[][] arr, int l, int r,
boolean ascend){
if(l>=r) return;
int i = l;
int j = r;
T[] key = arr[l];
while(i<j) {
while(i<j&&compare(key[0], arr[j][0], ascend)) j--;
arr[i] = arr[j];
while(i<j&&compare(key[0], arr[j][0], !ascend)) i++;
arr[j] = arr[i];
}
arr[i] = key;
sortHelper(arr, l, i-1, ascend);
sortHelper(arr, i+1, r, ascend);
}
private static<T extends Comparable<? super T>> boolean compare(T x, T y, boolean ascend) {
return ascend ? x.compareTo(y) < 0 : x.compareTo(y) > 0;
}
public static void main(String args[]) {
Integer[][] a = new Integer[4][];
a[0] = new Integer[]{5,6};
a[1] = new Integer[] {1,2,3};
a[2] = new Integer[] {4,5,6,7};
a[3] = new Integer[] {8,9};
sort(a);
for(Integer[] i : a) {
System.out.println(Arrays.toString(i));
}
}
}
追问虽然看不很懂,但还是谢谢你
本回答被提问者采纳