You want to sort a list of a number of integers. Let's call the number of integers that will arrive to be , even though you don't know what its value. A special property of this list is that all of them are different, and they all are in the range . The list will be delivered online (that is, you only have access to the next item in the list). You also know that the list is huge, so holding the entire list in your memory would be a big problem; you want to keep the memory used as low as possible. How much additional memory do you need?
Details and Assumptions :
Your program has access to an input and an output (input is read-only and output is write-only). Your program can request the next number in the list from the input, and your program can print a number to the output; both of them don't require any additional memory except what you use to store the number read/written. You don't know the number of integers that will arrive; when you read
-1
, it means the input has stopped. You must print the integers to the output in the order you wish (that is, sorting them, from smallest to largest). Memory used is measured in the most number of bits used at once during the runtime of your program.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The answer is Θ ( lo g n ) . The following program uses only that much additional memory:
There are n + 1 possible integers, and n of them appear in the input. Instead of storing everything, we can instead try to look for the missing number. This can be done by adding the entire input, and comparing it to 0 + 1 + 2 + … + n ; the deficit gives the missing number. We can now print 0 , 1 , 2 , … , n in order, skipping the missing number.
The memory used is only for
sum
,expected_sum
,count
,k
, andi
. We know thatcount
,k
,i
are bounded above by n , so they use lo g n bits each. Additionally,sum
andexpected_sum
are bounded above by 0 + 1 + … + n = 2 n ( n + 1 ) = Θ ( n 2 ) , so they use 2 lo g n bits each. In total, that's 7 lo g n = Θ ( lo g n ) bits of extra memory.We also need at least Θ ( lo g n ) bits for obvious reasons: whatever number we read, we might need to store it (because the only other option is to immediately pump it out to output, which wouldn't sort the list). Thus this is indeed the optimal solution.