I have implemented the following code, but it does not seem to work for the case that my array has duplicate values. 

```
 private int partition(Integer[] arr,int left, int right)
 {
    int i = left;
    int j = right;
    int pivot = arr[left];

    while(true) 
    {
        while(arr[i] <pivot) i++;
        while(arr[j] > pivot) j--;

        if(i < j)
        {
            print(arr);
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        else return j; 
    }
}

public void quickSort(Integer[] arr, int left,int right)
{
    print(arr);
    if(left >= right) return;

    int index = partition(arr,left,right);

    quickSort(arr,left,index-1);
    quickSort(arr,index+1,right);
}

```

I found a slightly different implementation that works pretty well in this case, but I don't understand why. Any help would be appreciated.

```
    private int partition(Integer[] arr, int left, int right)
    {
    int i = left-1;
    int j = right+1;
    int pivot = arr[left];

    while(true) 
    {

        while(arr[++i] < pivot) ;
        while(arr[--j] > pivot) ;

        if(i < j)
        {
            print(arr);
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        else return j; 
     }
     }

    public void quickSort(Integer[] arr, int left,int right)
    {
    print(arr);
    if(left >= right) return;

    int index = partition(arr,left,right);

    quickSort(arr,left,index);
    quickSort(arr,index+1,right);
    }

```