-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
31 lines (26 loc) · 740 Bytes
/
Copy pathInsertionSort.java
File metadata and controls
31 lines (26 loc) · 740 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
30
//insert by defining a key into a sorted array starting
//from the end of the sorted array
class InsertionSort {
public static void main(String[] args) {
int[] array = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
insertionSort(array);
printArray(array);
}
public static void insertionSort ( int[] array) {
for ( int i = 1; i < array.length; i++ ) {
int key = array[i];
int j = i - 1;
while ( j >= 0 && array[j] > key ) {
array[j+1] = array[j];
j--;
}
array[j+1] = key;
}
}
private static void printArray(int[] input) {
for (int i = 0; i < input.length - 1; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(input[input.length-1]);
}
}