简体   繁体   English

我应该使用Java集合sort()还是自己实现?

[英]Should I use java collection sort() or implement my own?

I have an array that I need to sort the values in increasing order. 我有一个数组,需要按升序对值进行排序。 The possible value inside the array are is between 1-9, there will be a lot of repeating value. 数组内的可能值在1到9之间,会有很多重复值。 (fyi: I'm working on a sudoku solver and trying to solve the puzzle starting with the box with least possibilities using backtracking) (仅供参考:我正在研究数独求解器,并尝试使用回溯以最小的可能性从盒子开始解决难题)

The first idea that comes to my mine is to use Shell Sort. 我的第一个想法是使用Shell Sort。

I did some look up and I found out that the java collection uses "modified mergesort"(in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). 我做了一些查找,发现java集合使用了“修改的mergesort”(如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。

So I wish to know if the differences in performance will be noticeable if I implement my own sorting algorithm. 因此,我想知道如果实现自己的排序算法,性能差异是否会明显。

If you only have 9 possible values, you probably want counting sort - the basic idea is: 如果只有9个可能的值,则可能需要对sort进行计数 -基本思想是:

  • Create an array of counts of size 9. 创建一个大小为9的计数数组。

  • Iterate through the array and increment the corresponding index in the count array for each element. 遍历数组并为每个元素递增计数数组中的相应索引。

  • Go through the count array and recreate the original array. 遍历count数组并重新创建原始数组。

The running time of this would be O(n + 9) = O(n) , where-as the running time of the standard API sort will be O(n log n) . 它的运行时间为O(n + 9) = O(n) ,其中-标准API排序的运行时间为O(n log n)

So yes, this will most likely be faster than the standard comparison-based sort that the Java API uses, but only a benchmark will tell you for sure (and it could depend on the size of your data). 因此,是的,这很有可能比Java API使用的基于比较的标准排序要快,但是只有一个基准可以肯定地告诉您(它可能取决于数据的大小)。


In general, I'd suggest that you first try using the standard API sort , and see if it's fast enough - it's literally just 1 line of code (except if you have to define a comparison function), compared to quite a few more for creating your own sorting function, and quite a bit of effort has gone into making sure it's as fast as possible, while keeping it generic. 通常,我建议您首先尝试使用标准API sort ,看看它是否足够快-实际上,它只有1行代码(除非您必须定义比较函数),而对于创建自己的排序功能,并在确保其通用性的同时付出了很多努力。

If that's not fast enough, try to find and implement a sort that works well with your data. 如果那还不够快,请尝试找到并实施一种适合您的数据的排序方式。 For example: 例如:

  • Insertion sort works well on data that's already almost sorted (although the running time is pretty terrible if the data is far from sorted). 插入排序在几乎已排序的数据上效果很好(尽管如果数据离排序很远,运行时间会很糟糕)。

  • Distribution sorts are worth considering if you have numeric data. 如果您有数字数据,则值得考虑分配排序


As noted in the comment, Arrays.parallelSort (from Java 8) is also an option worth considering, since it multi-threads the work (which sort doesn't do, and is certainly quite a bit of effort to do yourself ... efficiently). 正如评论中指出的那样, Arrays.parallelSort (来自Java 8)也是一个值得考虑的选项,因为它对工作进行了多线程处理(哪种sort不起作用,并且肯定要花很多精力做自己的事情……)有效率的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM