Sorts a portion of the array using insertion sort. @param arr the input array @param begin the starting index @param end the ending index
(int[] arr, int begin, int end)
| 149 | * @param end the ending index |
| 150 | */ |
| 151 | public static void insertionSort(int[] arr, int begin, int end) { |
| 152 | if (arr == null || arr.length < 2) { |
| 153 | return; |
| 154 | } |
| 155 | for (int i = begin + 1; i != end + 1; i++) { |
| 156 | for (int j = i; j != begin; j--) { |
| 157 | if (arr[j - 1] > arr[j]) { |
| 158 | swap(arr, j - 1, j); |
| 159 | } else { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Swaps two elements in an array. |