-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.java
More file actions
45 lines (27 loc) · 822 Bytes
/
Copy pathinsertion.java
File metadata and controls
45 lines (27 loc) · 822 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
package arrays;
public class insertion {
public static void main(String[] args) {
int arr[] = { 2, 3, 434, 22, 422, 44, 111 };
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
int n = arr.length;
for (int i = 1; i < n; i++) {
int curr = arr[i];
int j;
for (j = i - 1; j >= 0 && arr[j] > curr; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = curr;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}