PHP Code:
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;
}
void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}
The reason why the range of the array decreases in size, is it because of the line of code "return i;"? Moreover, does "index - 1" make it so that the left range of the array decreases in size faster than the right one? Last question: Why won't the program work if I write "index" instead of "index - 1"? I know that there would be a conflict in the algorithm, but I don't know exactly why it wouldn't work.