-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.java
More file actions
47 lines (36 loc) · 1.1 KB
/
InsertSort.java
File metadata and controls
47 lines (36 loc) · 1.1 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
package com.chen.test;
/**
* @author : chen weijie
* @Date: 2020-05-25 10:23
*/
public class InsertSort {
public static void testSort(int [] array){
for (int i = 1; i < array.length; i++) {
int temp = array[i];
int left = i - 1;
while (left >= 0 && temp < array[left]) {
array[left + 1] = array[left];
left--;
}
array[left + 1] = temp;
}
}
//遍历显示数组
public static void display(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] array = {6,8,1,2,4};
//未排序数组顺序为
System.out.println("未排序数组顺序为:");
display(array);
System.out.println("-----------------------");
testSort(array);
System.out.println("-----------------------");
System.out.println("经过插入排序后的数组顺序为:");
display(array);
}
}