-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortInsert.java
More file actions
41 lines (30 loc) · 886 Bytes
/
Copy pathSortInsert.java
File metadata and controls
41 lines (30 loc) · 886 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
package org.example;
import java.util.ArrayList;
public class SortInsert {
// 问题代码,好好分析下
// 下标有问题 ,j
public static int[] sort(int[] arr) {
int[] b = new int[arr.length];
b[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
int j = i - 1;
System.out.println("i = " + i + ", j= " + j);
while (j > 0 && b[j] > arr[i]) {
b[j] = b[j - 1];
j--;
}
System.out.println(", j= " + j);
if (j > 0) {
b[j] = arr[i];
}
}
return b;
}
public static void main(String[] args) {
int[] temp = {1,3,2,9,8,7,4,3,5};
int [] res = SortInsert.sort(temp);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
}