-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.java
More file actions
39 lines (34 loc) · 1021 Bytes
/
InsertSort.java
File metadata and controls
39 lines (34 loc) · 1021 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
package algorithms.sort;
import structires.collections.Collections;
import structires.table.single.SingleLinkedList;
import java.util.Random;
/**
* Created : zzc
* Time : 2017/9/26
* Email : zzcm159@gmail.com
* Description :插入排序
*/
public class InsertSort {
public static void main(String arg[]) {
int arr[] = ArrUtils.randomArr(20);
int[] sortedArr = insertSort(arr);
for (int i = 0; i < sortedArr.length; i++) {
System.out.println(sortedArr[i]);
}
}
public static int[] insertSort(int[] arr) {
if (arr != null && arr.length > 1) {
int size = arr.length;
for (int i = 0; i < size - 1; i++) {
for (int j = size - 1; j > i; j--) {
if (arr[j] < arr[i]) {
arr[j] += arr[i];
arr[i] = arr[j] - arr[i];
arr[j] -= arr[i];
}
}
}
}
return arr;
}
}