Microsoft interview question

How would you sort an array if you had infinite RAM? Infinite memory?

Interview Answers

Anonymous

30 Sept 2012

I would probably use counting sort. This is not generally used outside of sorting a bunch of integer numbers that fall within a small range precisely because it uses a lot of memory. For e.g., to sort number in the range of 1 to 10,000, you will need an array C[1...10,000]. It can be extended to sort a set of any kind of stuff that has total ordering relationship between them, and is O(n) time. Only down side is that memory used will be insane depending on the range of values whatever you are sorting will take.

3

Anonymous

13 Nov 2012

If no duplicates exist, create an infinite array, put each integer in the array by using the value of the integer as the index to put the value in the array, then read the array from left to right ignoring null values. N write + N read = O(n) time complexity.

1

Anonymous

28 Aug 2013

counting sort!