几种排序算法的实验性能比较

实现插入排序(Insertion Sort),自顶向下归并排序(Top-down Mergesort),自底向上归并排序(Bottom-up Mergesort),随机快速排序(Random Quicksort),Dijkstra 3-路划分快速排序(Quicksort with Dijkstra 3-way Partitioning)。对不同输入规模的数据进行实验,对比上述排序算法的时间及空间占用性能。

我的计算机:

  • CPU:Intel® Core™ i5-8265U CPU @ 1.60GHz × 8
  • OS:Ubuntu 20.04.1 LTS, 64位

运行时间

计算方法:在算法前后分别使用一次Java库函数System.currentTimeMillis()得到startTimeendTime,然后endTime - startTime即可。

表格形式

Comparison of running time of sorting algorithms (in Micro Seconds)

N=1000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort99112111112.89
MergeSort(top-down)10000010000.22
MergeSort(bottom-up)21111100110.89
QuickSort10000001000.22
QuickSort with 3-way partitioning11001010000.44
N=10000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort18916992103847980837978106.44
MergeSort(top-down)63333111212.56
MergeSort(bottom-up)591281222214.78
QuickSort53121211121.89
QuickSort with 3-way partitioning515512222214
N=100000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort11956126589770976398089803975597629820982210343.89
MergeSort(top-down)5329161617201716161722.22
MergeSort(bottom-up)5919171717181817171722.11
QuickSort3820161515151516151518.33
QuickSort with 3-way partitioning4322202120202120202023.00

折线图形式

内存占用

计算方法:在算法前后分别使用一次Java库函数Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()得到startSpaceendSpace,然后endSpace - startSpace即可。注:因为Java有垃圾回收机制,这一方法也许并不十分准确,但是用于比较算法,这一方法还是能给出大致的结果。

表格形式

Comparison of space usage of algorithms (in Kilo Bytes)

N=1000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort00000000000
MergeSort(top-down)00000000000
MergeSort(bottom-up)00000000000
QuickSort00000000000
QuickSort with 3-way partitioning00000000000
N=10000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort00000000000
MergeSort(top-down)039.08000039.08039.08013.03
MergeSort(bottom-up)039.080039.08039.08039.08017.37
QuickSort00000000000
QuickSort with 3-way partitioning00000000000
N=100000Run1Run2Run3Run4Run5Run6Run7Run8Run9Run10Average
InsertionSort00000000000
MergeSort(top-down)390.64390.64390.64390.64390.64390.64390.64390.64390.64390.64390.64
MergeSort(bottom-up)390.64390.64390.64390.64390.64390.64390.64390.64390.64390.64390.64
QuickSort00000000000
QuickSort with 3-way partitioning0614.410000000068.27

折线图形式

参考

How to measure the memory usage of data mining algorithms in Java?