Recursively sorts the array in a circular manner by comparing elements from the start and end of the current segment. @param The type of elements in the array, which must be comparable @param array The array to be sorted @param left The left boundary of the current segment being sorted @para
(final T[] array, final int left, final int right)
| 26 | * @return true if any elements were swapped during the sort; false otherwise |
| 27 | */ |
| 28 | private <T extends Comparable<T>> boolean doSort(final T[] array, final int left, final int right) { |
| 29 | boolean swapped = false; |
| 30 | |
| 31 | if (left == right) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | int low = left; |
| 36 | int high = right; |
| 37 | |
| 38 | while (low < high) { |
| 39 | if (SortUtils.greater(array[low], array[high])) { |
| 40 | SortUtils.swap(array, low, high); |
| 41 | swapped = true; |
| 42 | } |
| 43 | low++; |
| 44 | high--; |
| 45 | } |
| 46 | |
| 47 | if (low == high && SortUtils.greater(array[low], array[high + 1])) { |
| 48 | SortUtils.swap(array, low, high + 1); |
| 49 | swapped = true; |
| 50 | } |
| 51 | |
| 52 | final int mid = left + (right - left) / 2; |
| 53 | final boolean leftHalfSwapped = doSort(array, left, mid); |
| 54 | final boolean rightHalfSwapped = doSort(array, mid + 1, right); |
| 55 | |
| 56 | return swapped || leftHalfSwapped || rightHalfSwapped; |
| 57 | } |
| 58 | } |