Multidimensional Quicksort
this show how quicksort works for an multidimensional double array.
class QuickSort{
public double[][] sort(double[][] array, int key, int down, int top) {
int i = down;
int j = top;
double x = array[(down + top) / 2][key];
do {
while (array[i][key] < x) {
i++;
}
while (array[j][key] > x) {
j--;
}
if (i <= j) {
double[] temp = new double[array[i].length];
for (int y = 0; y < array[i].length; y++) {
temp[y] = array[i][y];
array[i][y] = array[j][y];
array[j][y] = temp[y];
}
i++;
j--;
}
} while (i <= j);
if (down < j) {
array = sort(array, key, down, j);
}
if (i < top) {
array = sort(array, key, i, top);
}
return array;
}
}
public double[][] sort(double[][] array, int key, int down, int top) {
int i = down;
int j = top;
double x = array[(down + top) / 2][key];
do {
while (array[i][key] < x) {
i++;
}
while (array[j][key] > x) {
j--;
}
if (i <= j) {
double[] temp = new double[array[i].length];
for (int y = 0; y < array[i].length; y++) {
temp[y] = array[i][y];
array[i][y] = array[j][y];
array[j][y] = temp[y];
}
i++;
j--;
}
} while (i <= j);
if (down < j) {
array = sort(array, key, down, j);
}
if (i < top) {
array = sort(array, key, i, top);
}
return array;
}
}
Created by
wohlgemuth
Last modified 2005-08-01 03:04 AM
Last modified 2005-08-01 03:04 AM