forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
Latest commit
29 lines (23 loc) · 668 Bytes
/
Copy pathInsertionSort.java
File metadata and controls
29 lines (23 loc) · 668 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
29
// Insertion sort in Java
importjava.util.Arrays;
classInsertionSort {
voidinsertionSort(intarray[]) {
intsize = array.length;
for (intstep = 1; step < size; step++) {
intkey = array[step];
intj = step - 1;
while (j >= 0 && key < array[j]) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
publicstaticvoidmain(Stringargs[]) {
int[] data = { 9, 5, 1, 4, 3 };
InsertionSortis = newInsertionSort();
is.insertionSort(data);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}