-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
28 lines (24 loc) · 896 Bytes
/
InsertionSort.java
File metadata and controls
28 lines (24 loc) · 896 Bytes
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
import java.util.Arrays;
public class InsertionSort{
private static Comparable[]array = {12,13,24,10,3,6,90,70};
public static void main(String[] args) {
InsertionSort insert = new InsertionSort();
insert.sort(array, 0, array.length);
System.out.println(Arrays.toString(array));
}
public void sort(Object[] a, int fromIndex, int toIndex) {
Object d;
int j;
for (int i = fromIndex + 1; i < toIndex; i++) {
d = a[i];
j = i;
while (j > fromIndex && ((Comparable) a[j - 1]).compareTo(d) > 0) {
a[j] = a[j - 1];
j--;}
a[j] = d; //! Remove comment or go to the readMe to understand the code better
//for(int k=0;k<array.length; k++){
// System.out.print(array[k] + ", ");}
//System.out.println();
}
}
}