forked from morethink/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.java
More file actions
41 lines (38 loc) · 919 Bytes
/
Insertion.java
File metadata and controls
41 lines (38 loc) · 919 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 algorithm.sort;
/**
* @author 李文浩
* @date 2018/1/25
*/
public class Insertion {
/**
* 通过交换进行插入排序,借鉴冒泡排序
*
* @param a
*/
public static void sort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j > 0; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
/**
* 通过将较大的元素都向右移动而不总是交换两个元素
*
* @param a
*/
public static void sort2(int[] a) {
for (int i = 1; i < a.length; i++) {
int num = a[i];
int j;
for (j = i; j > 0 && num < a[j]; j--) {
a[j] = a[j - 1];
}
a[j] = num;
}
}
}