QuickSort: I do not know why it enters the cycle, if the condition is not met

0

I'm trying out the QuickSort and making it trace to understand how it works, but I found something weird:

public static void QuickSort(Comparable[] array){
    int n = array.length;
    QuickSort(array, 0, n - 1);
}
private static void QuickSort(Comparable[] array, int d, int h){
    int i = d;
    int j = h;
    Comparable piv = array[(i + j) / 2];
    do{
        while(array[i].compareTo(piv) < 0)
            i++;
        while(array[j].compareTo(piv) > 0)
            j--;
        if(i <= j){
            Comparable temp = array[i];
            array[i++] = array[j];
            array[j--] = temp;
        }
    }
    while(i < j);
    if(d < j)
        QuickSort(array, d , j);
    if(i < h)
        QuickSort(array, i, h);
}

Doing the trace shows me that i = 5 and j = 4 and the condition of the while is i<j , Because it enters the while ?, if in the line: if(i <= j) , the condition was not met and NOT entered. Why do I enter one and not another? And the method works well without any problem. I leave a screenshot:

    
asked by Andry_UCI 31.03.2018 в 18:57
source

0 answers