算法分析入门系列(一) 排序算法

算法分析入门系列(一) 排序算法

排序算法

main函数代码

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
62
63
64
65
66
67
68
69
70
public static void main(String[] args) {
int max = 10;
System.out.println("插入排序-------------------------------------");
int[] num = randomCreate(max);
printResult(num);
Long bt = System.nanoTime();
int[] temp1 = insertSort(num);
Long et = System.nanoTime();
System.out.println("排序用时:" + (et - bt + ""));
printResult(temp1);


System.out.println("合并排序-------------------------------------");
int[] temp4 = randomCreate(max);
printResult(temp4);
Long bt2 = System.nanoTime();
mergeSort(temp4);
Long et2 = System.nanoTime();
System.out.println("排序用时:" + (et2 - bt2 + ""));
printResult(temp4);


System.out.println("快速排序-------------------------------------");
int[] temp5 = randomCreate(max);
printResult(temp5);
Long bt3 = System.nanoTime();
quickSort(temp5, 0, temp5.length - 1, false);
Long et3 = System.nanoTime();
System.out.println("排序用时:" + (et3 - bt3 + ""));
printResult(temp5);

System.out.println("随机化快速排序--------------------------------");
int[] temp9 = randomCreate(max);
printResult(temp9);
Long bt7 = System.nanoTime();
quickSort(temp9, 0, temp9.length - 1, true);
Long et7 = System.nanoTime();
System.out.println("排序用时:" + (et2 - bt2 + ""));
printResult(temp9);


System.out.println("桶排序---------------------------------------");
int[] temp6 = randomCreate(max);
printResult(temp6);
Long bt4 = System.nanoTime();
temp6 = bucketSort(temp6, max);
Long et4 = System.nanoTime();
System.out.println("排序用时:" + (et4 - bt4 + ""));
printResult(temp6);

System.out.println("计数排序-------------------------------------");
int[] temp7 = randomCreate(max);
printResult(temp7);
Long bt5 = System.nanoTime();
temp7 = countSort(temp7);
Long et5 = System.nanoTime();
System.out.println("排序用时:" + (et5 - bt5 + ""));
printResult(temp7);

System.out.println("基数排序-------------------------------------");
int[] temp8 = randomCreate(max);
printResult(temp8);
Long bt6 = System.nanoTime();
radixSort(temp8, (max + "").length());
Long et6 = System.nanoTime();
System.out.println("排序用时:" + (et6 - bt6 + ""));
printResult(temp8);
System.out.println("---------------------------------------------");

}
阅读更多
完成经典算法的C语言实现
数据结构入门?这一篇就够了