何时使用合并排序和何时使用快速排序? [英] When to use merge sort and when to use quick sort?

查看:325
本文介绍了何时使用合并排序和何时使用快速排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科文章中的合并排序



维基百科文章快速排序



两篇文章都有很好的可视化。



两者都有n * log(n)的复杂性。



速度的排序。我的猜测是,由于比较可以快速比较任何两个值,不管它们的传播,数据值的范围并不重要。



更重要的是,考虑横向分布(x方向)相对于排序(去除的幅度)。



要考虑的一个好的测试用例是如果测试数据具有某种程度的排序。 ..

解决方案

虽然Java 6和早期版本使用合并排序作为排序算法,但C#使用QuickSort作为排序算法。 / p>

QuickSort的性能优于合并排序,即使它们都是O(nlogn)。 QuickSort的常数小于合并排序。


The wikipedia article for merge sort.

The wikipedia article for quick sort.

Both articles have excellent visualizations.

Both have n*log(n) complexity.

So obviously the distribution of the data will effect the speed of the sort. My guess would be that since a comparison can just as quickly compare any two values, no matter their spread, the range of data values does not matter.

More importantly one should consider the lateral distribution (x direction ) with respect to ordering (magnitude removed).

A good test case to consider would be if the test data had some level of sorting...

解决方案

While Java 6 and earlier versions use merge sort as the sorting algorithms, C# uses QuickSort as the sorting algorithm.

QuickSort performs better than merge sort even though they are both O(nlogn). QuickSort's has a smaller constant than merge sort.

这篇关于何时使用合并排序和何时使用快速排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆