-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
36 lines (29 loc) · 1.1 KB
/
InsertionSort.java
File metadata and controls
36 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package sorting_searching;
import java.util.Comparator;
public class InsertionSort {
public static void main(String[] args) {
// Example:
int[] array = {91, 34, 86, 39, 27, 87, 70, 48, 100, 95};
insertionSort(array);
System.out.println(java.util.Arrays.toString(array)); // [27, 34, 39, 48, 70, 86, 87, 91, 95, 100]
}
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; ++i) {
int k = -(BinarySearch.binarySearchIterative(a, a[i], 0, i - 1) + 1);
int temp = a[i];
System.arraycopy(a, k, a, k + 1, i - k);
a[k] = temp;
}
}
public static <T extends Comparable<? super T>> void insertionSort(T[] a) {
insertionSort(a, Comparator.naturalOrder());
}
public static <T> void insertionSort(T[] a, Comparator<T> comp) {
for (int i = 1; i < a.length; ++i) {
int k = -(BinarySearch.binarySearchIterative(a, a[i], comp, 0, i - 1) + 1);
T temp = a[i];
System.arraycopy(a, k, a, k + 1, i - k);
a[k] = temp;
}
}
}