Skip to content

Metabolomics Fiehn Lab

Sections
Personal tools
You are here: Home » Members » Gert Wohlgemuth » java » Multidimensional Quicksort

Multidimensional Quicksort

Document Actions
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;
    }
}
Created by zwluxx
Last modified 2005-08-01 03:04 AM
 

Powered by Plone

This site conforms to the following standards: