简体   繁体   English

如何对大量的int进行排序?

[英]How to sort a large array of ints?

During job interview I was asked the following question: 在求职面试中,我被问到以下问题:

We have a client application that can send a request and receive a data stream of ints (maybe large, but less than INT_MAX). 我们有一个客户端应用程序可以发送请求并接收一个int数据流(可能很大,但小于INT_MAX)。 We need to do this: 我们需要这样做:

Int Data  ----> Our  ----> Sorted Int Data
Stream          App        Data Stream

So I would write the method as follows: 所以我会按如下方式编写方法:

public int[] sort(int[] array){
   Arrays.sort(array);
   return array;
}

The problem is that the large array cannot fit into stack and will be put into heap which decrease performance. 问题是大型 array不能装入堆栈并将被放入堆中 ,这会降低性能。 How to refactor it in a good-performance way ? 如何以良好的性能重构它?

Independent of the programming language, the usual way of sorting large amounts of data is the following: 独立于编程语言,通常的方法是对大量数据进行排序如下:

  • only sort a chunk of the data 只排序一大块数据
  • merge all the sorted chunks using merge sort. 使用合并排序合并所有排序的块。

Some optimized implementations even perform insertion sort or something alike on datasets that roughly fit into the CPU's cache (eg timsort). 一些优化的实现甚至可以对大致适合CPU缓存的数据集执行插入排序或类似的操作(例如,timsort)。

However, since the data does fit into RAM, Java's native implementation should be already pretty much as fast as it gets. 但是,由于数据确实适合RAM,因此Java的本机实现应该已经快得多了。 If it exceeds RAM, or you want to limit the RAM usage, you'll have to use external sorting . 如果它超过RAM,或者您想限制RAM使用,则必须使用外部排序 But that is definetely slower, because it goes to disk 但这确实比较慢,因为它会进入磁盘

Well....if they ask you to how to sort data and don't provide the data to be sorted, then Arrays.sort() should work fine. 好吧....如果他们问你如何排序数据并且不提供要排序的数据,那么Arrays.sort()应该可以正常工作。 However, the best way to sort depends on the data, Quicksort and Insertion are the fastest for sorting Arrays of Integers, but for floating point arrays, you would need a specialized sort method. 但是,排序的最佳方式取决于数据,Quicksort和Insertion是排序整数数组最快的,但对于浮点数组,您需要一种专门的排序方法。

https://en.wikipedia.org/wiki/Sorting_algorithm https://en.wikipedia.org/wiki/Sorting_algorithm

^ That is a full list of many acceptable ways of sorting algorithms, with the mathematical downside to each. ^这是许多可接受的排序算法排序的完整列表,每个都有数学上的缺点。

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

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