Skip to content

Commit 584645b

Browse files
committed
修改插入排序的实现
1 parent 40bf81b commit 584645b

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

src/com/fantasy/algorithm/sort/InsertionSort.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* <pre>
1919
* author : Fantasy
20-
* version : 1.0, 2020-08-31
20+
* version : 1.1, 2020-09-03
2121
* since : 1.0, 2020-08-31
2222
* </pre>
2323
*/
@@ -36,27 +36,8 @@ public static void insertionSort(int[] arr) {
3636
int j;
3737
for (int i = 1; i < length; i++) {
3838
value = arr[i];
39-
for (j = i - 1; j >= 0 && arr[j] > value; j--) {
40-
arr[j + 1] = arr[j];
41-
}
42-
arr[j + 1] = value;
43-
}
44-
}
45-
46-
/**
47-
* 插入排序的另一种实现
48-
*
49-
* @param arr 数组
50-
*/
51-
public static void insertionSort2(int[] arr) {
52-
int length = arr.length;
53-
int value; // 待插入的值
54-
for (int i = 1; i < length; i++) {
55-
value = arr[i];
56-
int j = i;
57-
while (j > 0 && arr[j - 1] > value) {
39+
for (j = i; j >= 1 && arr[j - 1] > value; j--) {
5840
arr[j] = arr[j - 1];
59-
j--;
6041
}
6142
arr[j] = value;
6243
}

0 commit comments

Comments
 (0)