-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathInsertionSort.java
61 lines (54 loc) · 1.38 KB
/
InsertionSort.java
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
50
51
52
53
54
55
56
57
58
59
60
61
package sorting;
import java.util.Random;
public class InsertionSort {
public static int[] generateRandomArray(int len) {
int[] nums = new int[len];
for(int i = 0; i < len; i++){
nums[i] = (new Random()).nextInt(200);
}
return nums;
}
public int[] insertionSort(int[] nums){
long lStartTime = System.currentTimeMillis();
for(int i = 1; i < nums.length; i++){
int j = i-1;
if(nums[i] > nums[j]) continue;
while(j >= 0 && nums[j] > nums[i]){
swap(nums, j, i);
j--;
i--;
}
}
long lEndTime = System.currentTimeMillis();
System.out.println("Elapsed milliseconds: " + (lEndTime - lStartTime));
return nums;
}
private void swap(int[] nums, int j, int i) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void main(String... args){
// System.out.println(2 >> 4);
// System.out.println(Integer.toBinaryString(32));
int[] nums = generateRandomArray(10);
InsertionSort test = new InsertionSort();
SortUtils.print(nums);
// test.swap(nums, 2, 0);
// test.print(nums);
SortUtils.print(test.insertionSortAgain(nums));
// test.print(test.insertionSort(nums));
}
public int[] insertionSortAgain(int[] nums){
for(int i = 1; i < nums.length; i++){
for(int j = i-1; j >= 0; j--){
if(nums[j+1] <= nums[j]){
int temp = nums[j+1];
nums[j+1] = nums[j];
nums[j] = temp;
}
}
}
return nums;
}
}