-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.Java
More file actions
50 lines (45 loc) · 913 Bytes
/
InsertionSort.Java
File metadata and controls
50 lines (45 loc) · 913 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
46
47
48
49
50
package java202;
import java.util.Arrays;
import java.util.Collections;
class InsertionSort {
public static void main(String[] args) {
var n = 5;
var a = new Integer[n];
for (var i = 0; i < n; i++) {
a[i] = i;
}
Collections.shuffle(Arrays.asList(a));
for (var i: a) {
System.out.printf("%3d", i);
}
System.out.println();
for (var i = 1; i < n; i++) {
var l = a[i];
for (var j = i; j > 0; j--) {
if (a[j-1] < l) {
a[j] = l;
break;
}
a[j] = a[j-1];
}
if (a[0] > l) {
a[0] = l;
}
}
/* while문
for (var i = 1; i < n; i++) {
var t = a[i];
var j = i;
while (j > 0 && t < a[j-1]) {
a[j] = a[j-1];
j -= 1;
}
a[j] = t;
}
*/
for (var i: a) {
System.out.printf("%3d",i);
}
System.out.println();
}
}