简体   繁体   English

用最小的时间复杂性在Java中排序

[英]Sorting in java with minimum time complexity

I am new to java i have to sort an array of order 10^6. 我是Java的新手,我必须对10 ^ 6的数组进行排序。 What is time complexity of sort method which is available in java. Java中可用的sort method时间复杂度是多少? Which sorting algorithm should use? 应该使用哪种排序算法?

Assuming you have no other knowledge of the data and have to use a generic sort method, the best theoretical in-place sort algorithm is O(n*log(n)). 假设您没有其他数据知识,而必须使用通用排序方法,那么最佳的理论上原位排序算法是O(n * log(n))。 The Arrays.sort method should be using one of those, and is your best choice without more info. Arrays.sort方法应该使用其中之一,并且是没有更多信息的最佳选择。

If you're willing to use a lot of memory, you can use a non-in place sort such as radix or counting. 如果您愿意使用大量内存,则可以使用非原位排序,例如基数或计数。 These can be quicker than n*log(n), some as quick as O(n), but may use O(n) or worse memory. 它们可能比n * log(n)快一些,有些比O(n)快,但可能会使用O(n)或更差的内存。 If you have knowledge of the data having special properties (such as it being almost already sorted) an insertion sort or similar algorithms could be quicker than O(n*log(n)) without using memory, but without more info one of those can't be suggested. 如果您了解具有特殊属性的数据(例如,几乎已经对其进行了排序),则插入排序或类似算法可能比不使用内存的O(n * log(n))更快,但是如果没有更多信息,那么其中一个可以不建议。

There are many different sorting algorithms and each with their own up and downsides. 有许多不同的排序算法,每种都有其自身的优缺点。 As far as I know there is no "sort algorithm collection" in Java that you could use so it's probably the best to implement the algorithm yourself. 据我所知,您在Java中没有可以使用的“排序算法集合”,因此,最好是自己实现该算法。

Here is a list of sorting algorighms with a table of their properties. 这是一个排序算法及其属性表的列表 Which one is the best for you depends on the resources given and the requirements of the application. 哪一种最适合您取决于所提供的资源和应用程序的要求。

根据要排序的数据,您可以选择排序算法,但是如果您不知道数据类型,则可以简单地使用Arrays.sort() O(n lg(n))。

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

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