Skip to content

Commit 60fda6b

Browse files
邵明邵明
authored andcommitted
Shell,Insert
1 parent 5b194d4 commit 60fda6b

File tree

11 files changed

+103
-5
lines changed

11 files changed

+103
-5
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.shellming.impl;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName InsertionSort
7+
* @Description TODO
8+
* @Author shaoming
9+
* @Date 2020/4/2 2:47 上午
10+
*/
11+
public class InsertionSort implements ArraySort {
12+
@Override
13+
public int[] sort(int[] sourceArray) {
14+
long t1 = System.currentTimeMillis();
15+
int[] res = Arrays.copyOf(sourceArray,sourceArray.length);
16+
for(int i = 1; i < res.length; ++i){
17+
for(int j = i; j > 0; --j){
18+
if(res[j] < res[j - 1]){
19+
int temp = res[j];
20+
res[j] = res[j - 1];
21+
res[j - 1] = temp;
22+
}
23+
}
24+
}
25+
System.err.println(this.getClass().getName() + " " + (System.currentTimeMillis() - t1));
26+
return res;
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.shellming.impl;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName InsertionSortII
7+
* @Description TODO
8+
* @Author shaoming
9+
* @Date 2020/4/2 4:50 下午
10+
*/
11+
public class InsertionSortII implements ArraySort {
12+
@Override
13+
public int[] sort(int[] sourceArray) {
14+
long t1 = System.currentTimeMillis();
15+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
16+
for (int i = 1; i < arr.length; ++i){
17+
int temp = arr[i];
18+
int j = i;
19+
while(j > 0 && temp < arr[j - 1]){
20+
arr[j] = arr[j - 1];
21+
j--;
22+
}
23+
if (i != j){
24+
arr[j] = temp;
25+
}
26+
}
27+
System.err.println(this.getClass().getName() + " " + (System.currentTimeMillis() - t1));
28+
return arr;
29+
}
30+
}

src/cn/shellming/impl/ShellSort.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.shellming.impl;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName ShellSort
7+
* @Description TODO
8+
* @Author shaoming
9+
* @Date 2020/4/3 3:22 上午
10+
*/
11+
public class ShellSort implements ArraySort {
12+
13+
@Override
14+
public int[] sort(int[] sourceArray) {
15+
long t1 = System.currentTimeMillis();
16+
int[] res = Arrays.copyOf(sourceArray,sourceArray.length);
17+
int gap = 1;
18+
while(gap < res.length / 3){
19+
gap = gap * 3 + 1;
20+
}
21+
while (gap > 0){
22+
for (int i = gap; i < res.length; i++) {
23+
int tmp = res[i];
24+
int j = i - gap;
25+
while (j >= 0 && res[j] > tmp) {
26+
res[j + gap] = res[j];
27+
j -= gap;
28+
}
29+
res[j + gap] = tmp;
30+
}
31+
gap = (int) Math.floor(gap / 3);
32+
}
33+
System.err.println(this.getClass().getName() + " " + (System.currentTimeMillis() - t1));
34+
return res;
35+
}
36+
}

src/cn/shellming/impl/test/Test.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.shellming.impl.test;
22

3-
import cn.shellming.impl.BubbleSort;
4-
import cn.shellming.impl.SelectionSort;
3+
import cn.shellming.impl.*;
54

65
import java.util.Arrays;
76
import java.util.Random;
@@ -14,11 +13,16 @@
1413
*/
1514
public class Test {
1615
public static void main(String[] args) {
17-
1816
BubbleSort sort = new BubbleSort();
19-
System.err.println(Arrays.toString(sort.sort(Test.randomNumber(10000))));
17+
System.err.println(Arrays.toString(sort.sort(Test.randomNumber(1000))));
2018
SelectionSort selectionSort = new SelectionSort();
21-
System.err.println(Arrays.toString(selectionSort.sort(Test.randomNumber(10000))));
19+
System.err.println(Arrays.toString(selectionSort.sort(Test.randomNumber(100000))));
20+
InsertionSort insertionSort = new InsertionSort();
21+
System.out.println(Arrays.toString(insertionSort.sort(Test.randomNumber(1000))));
22+
InsertionSortII ii = new InsertionSortII();
23+
System.out.println(Arrays.toString(ii.sort(Test.randomNumber(10))));
24+
ShellSort shellSort = new ShellSort();
25+
System.out.println(Arrays.toString(shellSort.sort(Test.randomNumber(10))));
2226
}
2327

2428
public static int[] randomNumber(int n){

0 commit comments

Comments
 (0)