Trying to understand this function:
public static int sort(int[] arr, int start, int end) {
int pivot = arr[end];
int pIndex = start;
for (int i = start; i < end; i++) {
if (arr[i] <= pivot) {
final int temp = arr[i];
arr[i] = arr[pIndex];
arr[pIndex] = temp;
pIndex++;
}
}
final int temp = arr[pIndex];
arr[pIndex] = pivot;
arr[end] = temp;
return pIndex;
}
public static void quickSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
// finding the pivot element
int pivot = sort(arr, start, end);
quickSort(arr, start, pivot - 1);
quickSort(arr, pivot + 1, end);
}
how does it find the pivot element and how does it sort the array? how does it sort sub arrays? and how does it finally change the index of the pivot?