Quicksort - StackOverflowException

0
public class QuickSort {

    public static void quickSort(int[] tab, int lowIndex, int highIndex) {
	if (tab.length == 0 || tab == null) {
	    return;
	}

	int i = lowIndex;
	int j = highIndex;

	int pivot = tab[tab.length / 2];

	while (i <= j) {
	    while (tab[i] < pivot) {
		i++;
	    }
	    while (tab[j] > pivot) {
		j--;
	    }

	    if (i <= j) {
		int c = tab[i];
		tab[i] = tab[j];
		tab[j] = c;
		i++;
		j--;
	    }

	    if (lowIndex < j) {
		quickSort(tab, lowIndex, j);
	    }
	    if (i < highIndex) {
		quickSort(tab, i, highIndex);
	    }
	}

    }

    public static void main(String[] args) {
	int[] t = new int[] { 1, 3, 2, 5, 1, 2, 7 };

	quickSort(t, 0, t.length - 1);

	for (int i : t) {
	    System.out.print(i);
	}

    }

}

Błąd w 30 linijce, co jest źle ?

Pozdrawiam

0

Źle wybierasz pivota bo pivot jest wybierany z fragmentu tablicy który przetwarzasz a nie cały czas z całej tablicy...

0

No tak, nie zauważyłem. Dzięki za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1