-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathinsertion_sort.java
More file actions
45 lines (36 loc) · 921 Bytes
/
Copy pathinsertion_sort.java
File metadata and controls
45 lines (36 loc) · 921 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class InsertionSorting {
private int[] array;
public InsertionSorting() {
setArray();
}
public static void main(String[] args) {
InsertionSorting is = new InsertionSorting();
System.out.println("Before Sorting array is");
is.printTheArray();
is.sortTheArray();
System.out.println("After Sorting array is");
is.printTheArray();
}
private void printTheArray() {
for(int i=0; i<array.length; i++) {
System.out.print(array[i]+" ");
}
System.out.println();
}
private void sortTheArray() {
int i,j, index;
for(i=0; i<array.length; i++) {
index = array[i];
j = i - 1;
while (j > -1 && array[j] > index) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = index;
}
}
private void setArray() {
int[] a = {72,21,69,38,96,77,30,19,42,55,99};
array = a;
}
}