MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / insertionSort

Method insertionSort

src/main/java/com/thealgorithms/others/BFPRT.java:151–164  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 1

getMedianMethod · 0.95

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected