-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.java
More file actions
37 lines (34 loc) · 878 Bytes
/
InsertSort.java
File metadata and controls
37 lines (34 loc) · 878 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
package Sort;
/**
* FileName: InsertSort
* Author: mac
* Date: 2019-01-21 15:14
* Description: 插入排序算法
*/
public class InsertSort {
//int[]{4,5,2,3,6}
public static int[] insertSort(int[] arr, int n) {
for (int i = 1; i < n; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
//交换
int m = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = m;
}
}
}
return arr;
}
public static int[] insertSort2(int[] arr, int n) {
for (int i = 1; i < n; i++) {
int e = arr[i];
int j;
for (j = i; j > 0 && arr[j - 1] > e; j--) {
arr[j] = arr[j - 1];
}
arr[j] = e;
}
return arr;
}
}