Sort

Sorting Algorithm

Summary

There are a lot of sorting algorithms, I have only done on a part of the exercises and tests, and there are a lot of tests that are not tested because of their time complexity (e.g. bogo sort) or implementation complexity (e.g. tim sort). Here are some of the tests I did:

  • Comparison sorts
    • binary_tree_sort
    • bubble_sort
    • comb_sort
    • heap_sort
    • insertion_sort
    • merge_sort
    • odd_even_sort
    • quick_sort
    • selection_sort
    • shell_sort
    • smooth_sort
  • Non-comparison sorts
    • bucket_sort
    • counting_sort
    • radix_sort
  • Other
    • sleep_sort

Overview

The following information is in my algorithm implementation.

Binary tree sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(nlog(n))O(nlog(n))O(nlog(n))O(n)Yesbinary_tree_sort.cc
Dynamic display

None.

Bubble sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n)O(n²)O(n²)O(1)Yesbubble_sort.cc
Dynamic display

1102665-20170722210236637-463403994.gif

Bucket sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n+k)O(n²)O(n+k)O(n+k)Yesbucket_sort.cc
Dynamic display

None.

Comb sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(nlog(n))O(n²)O(n²/(2^p))O(1)Nocomb_sort.cc
Dynamic display

1102665-20170722210320731-17067064.gif

Counting sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
-O(n+r)O(n+r)O(n+r)Yescounting_sort.cc
Dynamic display

None.

Heap sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(nlog(n))O(nlog(n))O(nlog(n))O(1)Noheap_sort.cc
Dynamic display

1102665-20170722210341715-2107333203.gif

Insertion sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n)O(n²)O(n²)O(1)Yesinsertion_sort.cc
Dynamic display

1102665-20170722210350903-1977641132.gif

Merge sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(nlog(n))O(nlog(n))O(nlog(n))O(n)Yesmerge_sort.cc
Dynamic display

1102665-20170722210403153-1792641606.gif

Odd even sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n)O(n²)O(n²)O(1)Yesodd_even_sort.cc
Dynamic display

1102665-20170722210411246-607409663.gif

Quick sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(nlog(n))O(n²)O(nlog(n))O(log(n))Noquick_sort.cc
Dynamic display

1102665-20170722210418996-605919601.gif

Radix sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n)-O(wn)O(w+n)Tesradix_sort.cc
Dynamic display

None.

Selection sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n²)O(n²)O(n²)O(1)Noselection_sort.cc
Dynamic display

1102665-20170722210430371-1210938258.gif

Shell sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n^(7/6))O(n^(4/3))-O(1)Noshell_sort.cc
Dynamic display

1102665-20170722210438700-424615884.gif

Sleep sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(1)--O(n)Nosleep_sort.cc
Dynamic display

None.

Smooth sort

Best time complexityWorst time complexityAverage time complexityMemoryStableSource code
O(n)O(nlog(n))O(nlog(n))O(1)Nosmooth_sort.cc
Dynamic display

1102665-20170722210450418-1675340422.gif


Test

I chose some of the sorting algorithms that are commonly used or work better to show their performances in various orders of magnitude, and compared them to std::sort. (Only for integers)

  • Small amount of data (In microseconds)
Sorting algorithm1632641285121024
Bubble sort0.718460μs2.323331μs8.742816μs30.240628μs451.183452μs1876.617118μs
Insertion sort0.354564μs0.662476μs1.502234μs4.077493μs39.589937μs134.445298μs
Selection sort0.615823μs1.726170μs4.404065μs13.939987μs150.167438μs493.693882μs
Merge sort4.292098μs9.013405μs18.521335μs39.645921μs155.457916μs311.587638μs
Heap sort0.802436μs1.642194μs4.208122μs10.273043μs48.155471μs102.982355μs
Quick sort1.427589μs2.202033μs4.758630μs9.097381μs40.513671μs84.237084μs
Shell sort0.690468μs1.091686μs2.957815μs6.680743μs36.912041μs87.633440μs
Smooth sort1.045032μs2.649904μs6.475469μs15.134310μs77.033824μs168.044959μs
Comb sort0.513186μs1.110347μs2.351323μs5.514413μs28.999652μs62.673958μs
std::sort0.438540μs0.821097μs2.621912μs5.924961μs31.798846μs66.770113μs
  • Large amount of data (In milliseconds)
Sorting algorithm100001000001000000
Bubble sort192.724614ms15324.636641ms551999.618190ms
Insertion sort11.071746ms1199.033438ms140489.051698ms
Selection sort39.881986ms3896.193842ms394233.806428ms
Merge sort3.423321ms32.797505ms336.168389ms
Heap sort1.547394ms17.826575ms282.484509ms
Quick sort1.045033ms8.998943ms78.881106ms
Shell sort1.200761ms15.585073ms180.192715ms
Smooth sort2.136252ms25.397649ms350.425244ms
Comb sort0.988116ms10.678459ms126.565752ms
std::sort0.845077ms10.250556ms104.314305ms

From the above table, we can see that (In my sorting algorithms implementation):

  • When the amount of data is less than 128, insertion_sort is more advantageous.
  • When the amount of data is between 128 and 10000, comb_sort is more advantageous.
  • When the amount of data is greater than 10000, quick_sort is more advantageous.

In order to get more accurate data, I have done a further test.

Sorting algorithm163264128256512102420484096819210000100000100000010000000100000000
Insertion sort0.401218μs0.690468μs1.558218μs4.198791μs13.090898μs45.300293μs167.597088μs518.410767μs2153.046876μs9295.592109μs-----
Comb sort0.606492μs1.250307μs2.836517μs6.494131μs16.561899μs36.277557μs84.600980μs163.528925μs330.892747μs747.338199μs0.901061ms12.528727ms140.981603ms1540.349918ms16413.311661ms
Quick sort1.166331μs2.286009μs4.870598μs10.459656μs22.356231μs47.968858μs102.991685μs180.827946μs386.783325μs804.982939μs0.942489ms10.484755ms91.543914ms865.948456ms8535.924019ms
std::sort0.485194μs0.951726μs2.799194μs6.596768μs14.947697μs33.711629μs74.971752μs138.485468μs308.657814μs629.482793μs0.817458ms9.687451ms100.566277ms961.007785ms9889.731343ms

Now I am sure that:

  • When the amount of data is less than 256, insertion_sort has a very good performance, even better than std::sort.
  • When the amount of data is between 256 and 10000, the performance of comb_sort is slightly better than quick_sort.
  • When the amount of data is between 256 and 100000, the performance of std::sort is the best.
  • When the amount of data is greater than 100000, the performance of std::sort is not as good as quick_sort.

How can I beat std::sort? So I referred to the practice of introsort, but did not control the split depth. I call it Impro sort, and now use it to compare with quick_sort and std::sort.

Sorting algorithm163264128256512102420484096819210000100000100000010000000100000000
Quick sort1.166331μs2.286009μs4.870598μs10.459656μs22.356231μs47.968858μs102.991685μs180.827946μs386.783325μs804.982939μs0.942489ms10.484755ms91.543914ms865.948456ms8535.924019ms
Impro sort0.466531μs0.886410μs2.108721μs6.326164μs12.363079μs27.637313μs59.771987μs125.935454μs278.136618μs479.678131μs0.617594ms7.230302ms84.720028ms724.386383ms7360.014444ms
std::sort0.485194μs0.951726μs2.799194μs6.596768μs14.947697μs33.711629μs74.971752μs138.485468μs308.657814μs629.482793μs0.817458ms9.687451ms100.566277ms961.007785ms9889.731343ms

From the latest test can be seen, no matter in which respect, Impro sort havs improved than Quick sort, that's why I call it Impro sort, which means "Improved sort". And in my test, Impro sort achieved a comprehensive victory, even faster than std::sort. Oh yeah! Although the results may not be so accurate, this shows that there is space for improvement in sorting algorithm, and we can do better through our efforts.

转载于:https://www.cnblogs.com/widerg/p/7222647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值