我想使用快速排序按特定列对二维数组进行排序。我已经能够对一维数组实现快速排序。
为了对二维数组进行此操作,我需要在QuickSort方法中包含指定列的参数。但我不知道该如何继续执行。
例如,初始数组为:
public class QuickSort1D {
public static void main(String[] args) {
int[] A = {9, 3, 10, 4, 1, 44, 12, 2, 90, 0};
int l = 0;
int r = A.length-1;
QuickSort(A, l, r);
for (int i = 0; i < A.length; i++){
System.out.print(A[i] + " ");
}
}
private static void QuickSort(int[] a, int l, int r) {
int i;
if (r > l){
i = partition(a, l, r);
QuickSort(a, l, i-1);
QuickSort(a, i+1, r);
}
}
private static int partition(int[] a, int l, int r) {
int v = a[r];
int i = l;
int j = r;
int temp;
while (i < j){
while (a[i] < v){
i = i + 1;
}
while ((i < j) && (a[j] >= v)){
j = j - 1;
}
temp = a[i];
if (i < j){
a[i] = a[j];
a[j] = temp;
}else{
a[i] = a[r];
a[r] = temp;
}
}
return i;
}
}
为了对二维数组进行此操作,我需要在QuickSort方法中包含指定列的参数。但我不知道该如何继续执行。
例如,初始数组为:
{{4, 1, 3},
{6, 0, 2},
{5, 9, 8}}
第二列排序后的数组应该是
{{6, 0, 2},
{4, 1, 3},
{5, 9, 8}}