-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSortDemo.java
More file actions
49 lines (44 loc) · 1.5 KB
/
InsertSortDemo.java
File metadata and controls
49 lines (44 loc) · 1.5 KB
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
42
43
44
45
46
47
48
49
package com.basic;
import com.utils.VerifyUtil;
/**
* @ClassName: InsertSortDemo
* @Author: Jack.Zhang
* @Description: #插入排序 代码演示
* @Date: 2021-11-22
*/
public class InsertSortDemo {
/**
* <p>
* 插入排序代码实现
* TODO 注意选择排序与插入排序的不同,
* 选择排序是在i位置后选择第i个最小/大的值插入到i位置,
* 插入排序是将第i位值向前移动,类似与冒泡,找到合适的位置,使得i位置之前数据是有序的。
* <p/>
* @param arr 未排序数组
* @return int[] 排序后数组
* @author Jack.Zhang 2021/11/22 16:03
* @since 1.0.0
* @see
*/
public static int[] insert(int[] arr) {
for (int i = 1; i < arr.length; i++) {
// 每次排序完成前i位都是有序的,所以一旦交换完成,arr[j] >= arr[j - 1] 时,那么在j->0之间的都是小于单签arr[j]的
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
arr[j - 1] = arr[j - 1] ^ arr[j];
arr[j] = arr[j - 1] ^ arr[j];
arr[j - 1] = arr[j - 1] ^ arr[j];
}else {
// 效率优化
break;
}
}
}
return arr;
}
// 验证
public static void main(String[] args) {
boolean verify = VerifyUtil.verify(InsertSortDemo::insert, true);
System.out.println(verify);
}
}