Skip to content

Commit 0404d52

Browse files
committed
add sort algorithms test and comparison result.
1 parent 593c006 commit 0404d52

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed

BENCH_RESULT.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 八大排序算法耗时对比
2+
3+
当int数组长度分别为 10, 30, 100, 300, 1000, 3000 时,使用排序算法对对应的数组进行排序,耗时结果如下。
4+
5+
6+
### Arrays of length 10
7+
8+
| Algorithm | Random | 95% sorted | Sorted |
9+
| :------------- | ---------: | ---------: | ---------: |
10+
| Insertion sort | 0.004615ms | 0.000785ms | 0.000023ms |
11+
| Shell sort | 0.007287ms | 0.002860ms | 0.002230ms |
12+
| Selection sort | 0.001599ms | 0.000809ms | 0.000059ms |
13+
| Heap sort | 0.002094ms | 0.002083ms | 0.002085ms |
14+
| Bubble sort | 0.004517ms | 0.000814ms | 0.000063ms |
15+
| Quicksort | 0.001377ms | 0.001586ms | 0.001786ms |
16+
| Merge sort | 0.004518ms | 0.000778ms | 0.000026ms |
17+
| Radix sort | 0.000644ms | 0.000646ms | 0.000645ms |
18+
19+
### Arrays of length 30
20+
21+
| Algorithm | Random | 95% sorted | Sorted |
22+
| :------------- | ---------: | ---------: | ---------: |
23+
| Insertion sort | 0.123324ms | 0.000604ms | 0.000054ms |
24+
| Shell sort | 0.139223ms | 0.026520ms | 0.025894ms |
25+
| Selection sort | 0.015083ms | 0.001262ms | 0.000680ms |
26+
| Heap sort | 0.017556ms | 0.017629ms | 0.018402ms |
27+
| Bubble sort | 0.122594ms | 0.001022ms | 0.000486ms |
28+
| Quicksort | 0.011668ms | 0.015174ms | 0.016193ms |
29+
| Merge sort | 0.123781ms | 0.000607ms | 0.000055ms |
30+
| Radix sort | 0.002077ms | 0.002150ms | 0.002108ms |
31+
32+
### Arrays of length 100
33+
34+
| Algorithm | Random | 95% sorted | Sorted |
35+
| :------------- | ---------: | ---------: | ---------: |
36+
| Insertion sort | 5.751262ms | 0.056025ms | 0.000166ms |
37+
| Shell sort | 5.378896ms | 0.393119ms | 0.337421ms |
38+
| Selection sort | 0.199311ms | 0.064833ms | 0.010262ms |
39+
| Heap sort | 0.203811ms | 0.203682ms | 0.205747ms |
40+
| Bubble sort | 5.898053ms | 0.061130ms | 0.004886ms |
41+
| Quicksort | 0.137751ms | 0.177324ms | 0.191210ms |
42+
| Merge sort | 5.882192ms | 0.056593ms | 0.000167ms |
43+
| Radix sort | 0.006145ms | 0.006067ms | 0.006044ms |
44+
45+
### Arrays of length 300
46+
47+
| Algorithm | Random | 95% sorted | Sorted |
48+
| :------------- | -----------: | ----------: | ---------: |
49+
| Insertion sort | 142.971101ms | 12.366163ms | 0.000506ms |
50+
| Shell sort | 105.500973ms | 13.471134ms | 4.468945ms |
51+
| Selection sort | 2.007829ms | 1.323869ms | 0.107584ms |
52+
| Heap sort | 2.159838ms | 2.144288ms | 2.174938ms |
53+
| Bubble sort | 142.216001ms | 12.115499ms | 0.040407ms |
54+
| Quicksort | 1.330195ms | 1.654192ms | 1.971377ms |
55+
| Merge sort | 144.561227ms | 12.521661ms | 0.000508ms |
56+
| Radix sort | 0.029518ms | 0.028453ms | 0.028831ms |
57+
58+
### Arrays of length 1000
59+
60+
| Algorithm | Random | 95% sorted | Sorted |
61+
| :------------- | ------------: | -----------: | ----------: |
62+
| Insertion sort | 5649.939294ms | 360.765524ms | 0.001362ms |
63+
| Shell sort | 4399.805254ms | 310.691632ms | 43.263064ms |
64+
| Selection sort | 23.631708ms | 22.496570ms | 1.237903ms |
65+
| Heap sort | 23.676803ms | 23.326309ms | 23.421163ms |
66+
| Bubble sort | 5807.787855ms | 363.755447ms | 0.438336ms |
67+
| Quicksort | 15.424505ms | 16.081330ms | 22.285714ms |
68+
| Merge sort | 5822.908516ms | 355.370353ms | 0.001390ms |
69+
| Radix sort | 0.097762ms | 0.097361ms | 0.126791ms |
70+

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
> 原文链接: [**八大排序算法总结与java实现** - iTimeTraveler](https://itimetraveler.github.io/2017/07/18/%E5%85%AB%E5%A4%A7%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E4%B8%8Ejava%E5%AE%9E%E7%8E%B0/)
55
6-
date: 2017-07-29 12:30:55
76

87
### 概述
98

@@ -378,8 +377,8 @@ private static void max_heapify(int[] arr, int limit){
378377
③. 堆排序的过程由n次第②步完成, 时间复杂度为O(nlgn).
379378

380379

381-
| 平均时间复杂度 | 最好情况 | 最坏情况 | 空间复杂度 |
382-
| ---------------- | ---------------- | ---------------- | ----- |
380+
| 平均时间复杂度 | 最好情况 | 最坏情况 | 空间复杂度 |
381+
| --------- | --------- | --------- | ----- |
383382
| O(nlog2n) | O(nlog2n) | O(nlog2n) | O(1) |
384383

385384
Tips: **由于堆排序中初始化堆的过程比较次数较多, 因此它不太适用于小序列.** 同时由于多次任意下标相互交换位置, 相同元素之间原本相对的顺序被破坏了, 因此, 它是不稳定的排序.
@@ -814,7 +813,8 @@ Tips: 基数排序不改变相同元素之间的相对顺序,因此它是稳
814813

815814
---
816815

817-
各种排序性能对比如下,有些排序未详细介绍,暂且放到这里:
816+
各种排序性能对比如下图,有些排序未详细介绍,暂且放到这里。
817+
实例测试结果可以看这里:[**八大排序算法耗时对比**](https://github.com/iTimeTraveler/SortAlgorithms/blob/master/BENCH_RESULT.md)
818818

819819

820820
| 排序类型 | 平均情况 | 最好情况 | 最坏情况 | 辅助空间 | 稳定性 |

src/main/java/com/example/Bench.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,21 @@ private static void executionTimeReport(int size) {
206206
int[] randomSample = generateSample(size, 100);
207207

208208
System.out.println(String.format(
209-
"Arrays of length %d\n" +
209+
"### Arrays of length %d\n" +
210210
"=================================================================\n" +
211-
"Algorithm | %14s | %14s | %14s\n" +
212-
"Insertion sort | %14s | %14s | %14s\n" +
213-
"Shell sort | %14s | %14s | %14s\n" +
214-
"Selection sort | %14s | %14s | %14s\n" +
215-
"Heap sort | %14s | %14s | %14s\n" +
216-
"Bubble sort | %14s | %14s | %14s\n" +
217-
"Quicksort | %14s | %14s | %14s\n" +
218-
"Merge sort | %14s | %14s | %14s\n" +
219-
"Radix sort | %14s | %14s | %14s\n",
211+
"| Algorithm | %14s | %14s | %14s |\n" +
212+
"| %3s | %14s | %14s | %14s |\n" +
213+
"| Insertion sort | %14s | %14s | %14s |\n" +
214+
"| Shell sort | %14s | %14s | %14s |\n" +
215+
"| Selection sort | %14s | %14s | %14s |\n" +
216+
"| Heap sort | %14s | %14s | %14s |\n" +
217+
"| Bubble sort | %14s | %14s | %14s |\n" +
218+
"| Quicksort | %14s | %14s | %14s |\n" +
219+
"| Merge sort | %14s | %14s | %14s |\n" +
220+
"| Radix sort | %14s | %14s | %14s |\n",
220221
size,
221222
"Random", "95% sorted", "Sorted",
223+
":--", "---:", "---:", "---:",
222224
execute(insertionSort, randomSample),
223225
execute(insertionSort, partiallySortedSample),
224226
execute(insertionSort, sortedSample),

0 commit comments

Comments
 (0)