forked from Gerkins/arithmeticSum
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInsertionSort.java
More file actions
38 lines (30 loc) · 847 Bytes
/
InsertionSort.java
File metadata and controls
38 lines (30 loc) · 847 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
/**
* Created by lrkin on 2016/10/27.
*
* 插入排序
*/
public class InsertionSort {
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+"-");
}
}
public void insertSort(int[] array) {
int temp , i ,j;
for (i = 1; i < array.length; i++) {
temp = array[i];
for (j = i - 1; j >= 0 && array[j] > temp ; j--) {
array[j+1] = array[j];
}
array[j+1] = temp;
}
}
public static void main(String[] args) {
InsertionSort insertionSort = new InsertionSort();
int[] array = {3,12,4,53,33,22};
insertionSort.print(array);
insertionSort.insertSort(array);
System.out.println("");
insertionSort.print(array);
}
}