I would like to ask a question for quick sort partition function(). If I replace the statement
int pivot = arr[(left + right) / 2];
with
int pivot = arr[left+(right-left)>>1];
The algorithm does not work when there is duplicated elements in the array. Why? Thanks.
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2]; **********
while (i <= j) {
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}