How to save each steps of quicksort in matrix in java?

Ondra Jahoda :

I have trouble saving each step of sorting quickstep. I wrote this but its not printing anything... I want to make javafx application with feature that i can push next button and it makes one step and back button which goes step back. So i will run the whole quicksort and save all the iterations to matrix and later i will be showing each column of the matrix bz clicking next and back. So i wanted to strat from beginning but im stuck in saving the steps.

Edit: P.S. I know should print the matrix each step and i dont want that, i will add some condition later.

public class QS {
    public static void main(String[] args) {
        int[] x = { 9, 2, 4, 7, 3, 7, 10 };
        System.out.println(Arrays.toString(x));

        final int [] [] test  = {
            {1,2,3},
            {4,5,6},
            {7,8,9}
        };

        //for (int p = 0; p < test.length; p++){
        //    for (int k = 0; k < test[p].length; k++){
        //        System.out.print(test[p][k] + " ");
        //    }
        //    System.out.println();
        //}

        int low = 0;
        int high = x.length - 1;
        quickSort(x, low, high);
        System.out.println(Arrays.toString(x));
        int [] [] save = new int [9] [x.length];
    }

    public static void quickSort(int[] arr, int low, int high) {
        if (arr == null || arr.length == 0)
            return;

        if (low >= high)
            return;

        // pick the pivot
        int middle = low + (high - low) / 2;
        int pivot = arr[middle];

        // make left < pivot and right > pivot
        int i = low, j = high;
        while (i <= j) {
            while (arr[i] < pivot)
                i++;

            while (arr[j] > pivot)
                j--;

            if (i <= j) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }

        int[][] save = new int[9][arr.length];
        int index = 0;
        // recursively sort two sub parts
        if (low < j)
            quickSort(arr, low, j);

        for (int h=0; h>arr.length+1; h++){
            save [index] [h] = arr [h];
            index++;

            for (int e = 0; e < save.length; e++) {
                for (int f = 0; f < save[e].length; f++) {
                    System.out.print(save[e][f] + " ");
                }

                System.out.println();
            }
        }

        if (high > i)
            quickSort(arr, i, high);

        for (int h=0; h>arr.length+1; h++){
            save [index] [h] = arr [h];
            index++;

            for (int e = 0; e < save.length; e++) {
                for (int f = 0; f < save[e].length; f++) {
                    System.out.print(save[e][f] + " ");
                }

                System.out.println();
            }
        }
    }
}
Anicet Rakotonirina :

In the for loops like this one:

for (int h=0; h>arr.length+1; h++){/*code*/}

Presumably, it should be a smaller-than sign:

for (int h=0; h<arr.length+1; h++){/*code*/}

Otherwise h is always bigger than arr.length+1, making the condition false and and the loop just stops there.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357978&siteId=1