Why is insertion sort always beating merge sort in this implementation?

房东的猫 提交于 2019-12-04 10:16:13

because, after the merge sort, the objects in elements are already sorted. do another

elements = GetFilledList(i, 0, Int32.MaxValue, false);

before the

sw.Restart();

Sorting 10000 elements isn't nearly enough to effectively evaluate an algorithm. Go bigger.

Also, is the input random? Post your implementation of GetFilledList

AND you need to unsort elements before doing insertion sort (or just re-initialize elements).

If you flip the order in which you do the sorts, what happens? I'm guessing that you are doing all the work in mergesort, and then insertion sort is merely sorting an already sorted list, which it's actually very good at (O(n), assuming a sane implementation).

An insertion sort should be faster than a merge sort for small inputs; that's how O(N) works.

f(n) = O(g(n)) if for all n, greater than n0, f(n) < C * g(n)

Algorithms like with good complexities typically have a higher C values, so they don't start actually beating "slower" algorithms until you get large inputs.

While esskar seems to have found the main problem you're facing, keep in mind that in the future you'll likely have to test algorithms with much, much larger inputs to really see the better algorithm shine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!