Not sure what you mean, "explain how recursion works." But here you go:
The function you posted takes an array of ints and two indexes. It will not sort the entire array, but only its part between the two indices, ignoring everything that is outside of them. This means that the same function can sort the entire array if you pass the first and last indices or just an auxiliary array if you pass the left value, which is not the index of the first element of the array and / or the right value, which is not the index of the last item.
A sorting algorithm is a well-known high-speed sorting. It uses a central element as a rod (it could use any other element). It splits the array into a subframe less than (or equal to) pivot and a submatrix greater than (or equal to) pivot , leaving the element equal to the axis of rotation between the two sections.
Then it recursively calls itself to sort the two sections, but only does it if necessary (hence, ifs before the recursive calls).
The implementation works, but is not optimal in many ways and can be improved. Here are some possible improvements:
- switch to another sorting algorithm if the array is short enough
- chose the rotation value as the average of three values ββ(usually the first, last and average)
- initially move one rotation value from the array (put it in the first or last position and reduce focus to the rest of the array), and then change the tests to transfer values ββequal to the axis to reduce the number of swaps with their participation. You will return the rotation value with the final exchange at the end. This is especially useful if you do not follow Proposition 2 and choose the first or last element instead of the middle, as in this implementation.
source share