Live data from Hacker News

Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

stackoverflow.com

51–60 of 63 posts

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#51

I'm a fan of the actual (in-RAM, no tricks) problem and its variants. The entropy of the sorted list is 0.96MiB so it is theoretically possible (see https://news.ycombinator.com/item?id=4679756 ), but I've not seen a good writeup of an actual algorithm. My personal favorite variant of this is sorting 1M 32-bit integers using 2MiB of RAM. This is possible and I coded it up for fun a few years ago. Here's the specific…

Interesting, sort of bucket sort using a linked list if I understand correctly. How do you determine the optimal number of bits - 12 for 32bit and 6 for 1e8 = 30bit?

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#52

Earlier quoted context omitted.

The classic programming interview version of this question is to infer from "telephone numbers" that they're all unique and so you just allocate the provided memory as a big bitfield, mark all the numbers you have in your input list, then scan through and read them back in order.

Interesting is this a counting sort then? Might you or someone else have a link that states the whole interview question?

> Interesting is this a counting sort then?

Not really as all the input values are unique. So counting wouldn't do any good.

It's basically using a bitfield long enough to encompass all possible values and then switching the bit of the input value.

As an example, if you are dealing with 3 digit numbers, then the max possible value is 999 and the min possible is 0. So you need a bitfield that is 1000 bits long. And if your input values are 999, 2, 123 and 7. Then you set the 999th bit, 2nd bit, 123rd bit and the 7th bit. Now when you read the bitfield from the 0th bit to the 999th bit, you will find 4 set bits - the 2nd bit, the 7th bit, the 123rd bit and the 999th bit. So instead of viewing them as ordinal numbers but cardinal numbers, we get 2, 7, 123 and 999 which is sorted.

Another way to view it is as setting the positions in an array. Instead of bits, lets say you have an array of 1000 ints all initialized to 0. Lets call this array MyBitArray. And if your input values are 999, 2, 123 and 7 then

MyBitArray[999] = 1;

MyBitArray[2] = 1;

MyBitArray[123] = 1;

MyBitArray[7] = 1;

Now if you pass through the array in order and check for the indices with the value set to 1, you'll see that indexes 2, 7, 123 and 999 are set to 1 which incidentally gives you your sorted list.

The difference between the array example and the bit example is that the bit example is much more space efficient. But the overall idea is the same.

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#54

I'm a fan of the actual (in-RAM, no tricks) problem and its variants. The entropy of the sorted list is 0.96MiB so it is theoretically possible (see https://news.ycombinator.com/item?id=4679756 ), but I've not seen a good writeup of an actual algorithm. My personal favorite variant of this is sorting 1M 32-bit integers using 2MiB of RAM. This is possible and I coded it up for fun a few years ago. Here's the specific…

I think the second solution from SO is almost the same as yours, including having begun with Golomb coding: http://preshing.com/20121026/1mb-sorting-explained

However, they explain that Golomb coding isn't quite enough for the 1M case and switch to arithmetic coding instead.

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#55

This is basically an ICMP-based version of sleep sort: https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort

"Once COUNTER reaches 1000000, you have all of the values stored in the incessant stream of ICMP requests" This sounds to me like... https://en.wikipedia.org/wiki/Delay_line_memory

It's complicated by the fact that ICMP packets do not necessarily arrive in order.

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#57
I have only ever answered one question on SO, and this was it. I used to be pretty active on some of the other sites (cstheory and the old theoretical physics stack exchange) but this answer got enough attention that I immediately vowed not to post to SO again, since it could only damage my track record there.

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#58

I have only ever answered one question on SO, and this was it. I used to be pretty active on some of the other sites (cstheory and the old theoretical physics stack exchange) but this answer got enough attention that I immediately vowed not to post to SO again, since it could only damage my track record there.

Thanks. Since you're here, I'd like to take the liberty of asking:

1. What was the inspiration behind that answer?

2. Have you stumbled upon other such quirky techniques elsewhere that rival yours?

3. Any interesting anectode about someone who put your solution in production / research paper / homework and later reached out to you for help?

Re: Sorting 1M 8-digit numbers in 1 MB of RAM (2012)

#59

I have only ever answered one question on SO, and this was it. I used to be pretty active on some of the other sites (cstheory and the old theoretical physics stack exchange) but this answer got enough attention that I immediately vowed not to post to SO again, since it could only damage my track record there.

Thanks. Since you're here, I'd like to take the liberty of asking: 1. What was the inspiration behind that answer? 2. Have you stumbled upon other such quirky techniques elsewhere that rival yours? 3. Any interesting anectode about someone who put your solution in production / research paper / homework and later reached out to you for help?

There have been a few edits to my answer since I originally posted it. The original version had a bit at the top about it being against the spirit of the question but intended to amuse. It's not a practical solution at all.

Re 1: I'm not sure there was any particular inspiration. I've worked on quantum computing since 2004, so I guess I spend a lot of time thinking how to make computers in weird ways. Plus I spent college working at a number of ISPs doing tech support, so I used to be reasonably up on networks.

Re 2: Yep. In my field there is this idea of quantum sneaker-net. This is far more useful than my answer and potentially solves a major problem in the field I work in, but is also totally off the wall.

The basic idea is that you can make an extremely high speed low latency network for quantum communication by shipping giant refridgerators around the world on ships (using a quantum effect and the existing [non-quantum] internet). The paper is here: https://www.nature.com/articles/srep36163

Re 3: Somebody here pointed out pingfs which seems to take the idea to a whole new extreme, but reading the website it seems like it was already being worked on in 2011. I doubt anyone implemented my answer in any way. It was somewhat intentionally impractical.

Post reply on HN