-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_Sort.java
More file actions
52 lines (41 loc) · 1 KB
/
Copy pathInsertion_Sort.java
File metadata and controls
52 lines (41 loc) · 1 KB
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
46
47
48
49
50
51
52
package sort;
public class Insertion_Sort {
public static void main(String[] args) {
int a[] = { 5, 2, 9, 1, 5, 6 };
for (int i = 1; i < a.length; i++) {
int curr = a[i];
int j = i - 1;
while (j >= 0 && curr < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = curr;
}
// c=a[1]2; c==2;
// j=0;
// 2<5
// a[j+1](a[1]=curr)
// [2,5,9,1,5,6]
// c=a[2]9
// j=1;
// 9<5
// exit loop with a[2]=9
// [2,5,9,1,5,6]
// 2nd it
// c==a[3]1
// j=2
// while loop
// 1<9
// a[3]-->(1) = a[2]-->(9)
// a[3]=1;
// j=2[2,5,1,9,5,6]
// 1<5
// a[3]=a[2]
// a[2]=1
// j=
//
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}