It is well known that loops and recursion, in many cases, can be converted to each other.
Then, the bubble sort (Bubble sort), as a typical double loop structure, can also be converted to recursive form.
However, converting recursion to a loop is beneficial for the program's operation because it avoids unpredictable "method stack" occurrences.
In most cases, this is not recommended, even if the recursive code might be very nice to implement. Beauty and efficiency are often conflicting.
So, the following bubble sort is for reference only, readers can use it to broaden their thinking. However, never write bubbles sort like this!
Public final class Bubblesortrecursion implements sort { @Override public void Sort (int[] array) { recursion (array, 0, array.length-2); } private void recursion (int[] array, int left, int. right) { if (right > 0) { if (Array[left] > array[left + 1 ] { Arrayhelper.swap (array, left, left + 1); } if (left >= right) { recursion (array, 0, right-1), or else { recursion (array, left + 1, right); } } }}
Sort algorithm (II.)--recursive implementation of bubble sort