St.Antario
St.Antario

Reputation: 27425

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). 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. How to refactor it in a good-performance way?

Upvotes: 4

Views: 4707

Answers (2)

Brenann
Brenann

Reputation: 94

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. 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.

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.

Upvotes: -1

Felk
Felk

Reputation: 8224

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 (e.g. timsort).

However, since the data does fit into RAM, Java's native implementation should be already pretty much as fast as it gets. If it exceeds RAM, or you want to limit the RAM usage, you'll have to use external sorting. But that is definetely slower, because it goes to disk

Upvotes: 12

Related Questions