views:

377

answers:

3

I'm interested in algorithm which I should use to meet these requirements (topic subj).

+2  A: 

Try this page: Sorting Algorithms. Besides showing nice animations of several algorithms it explains how they work and their complexity.

Steph
+3  A: 

Check out external merge sort algorithm.

Nick D
+2  A: 

Constantius,

If you're after an algorithm for that type of sorting (where the data can't all fit into core at once), my solution comes from the very earliest days of the "revolution" when top-end machines had less memory than most modern-day calculators.

I haven't worked out the big-O properties but I think it would be O(n) reads, O(n log n) sort phase (depends on the sort method chosen) and O(n) writes.

Let's say your data set has one million elements and you can only fit 100,000 in memory at a time. Here's what I'd do:

  • read in the first 100,000, sort them and write that sorted list back out.
  • do this for each group of 100,000.
  • run a merge operation on the 10 groups.

In other words, once your 10 groups are sorted within the group, grab the first entry from each group.

Then write that the lowest of those 10 (which is the lowest of the whole million) to the output file and read the next one from that group in its place.

Then just continue selecting the lowest of the 10, writing it out and replacing it from the same group. In that way, the final output is the entire sorted list of a million entries.

paxdiablo
Good answer, but that's probably the kind of thing his teacher could tell him no? I think he would expect code if he posts a question here. Just giving my opinion, it's still a good answer.
toto
Possibly but since an algorithm was asked for (even if was in C++), and it's tagged homework, I'm not keen on just doing the work for them. It's going to do the questioner more good in the long run to learn how to learn rather than just being given answers.
paxdiablo