I\'m trying to understand how external merge sort algorithm works (I saw some answers for same question, but didn\'t find what I need). I\'m reading the book \"Analysis Of A
You'll iterate through the files at the same time.
Just start from the beginning of each file and keep picking whichever file's element is not greater (i.e. smaller or equal) than the other, output that element to the new file and increase the iterator.
From your last statement, it's unclear whether or not you already know to do this, but this is all you need to do, because:
You'd only need to have one number in memory for each of the files, and of course any indices and other variables that are presumably ignored for the purpose of this exercise.
You only need to read each file once, as you can keep the files open at the correct position during this process so you don't need to read the whole file again to get to the correct position.
So, for:
A:[1,2,3,5]
B:[4,6,8,9]
You'd start off with the first element from each file - 1 and 4.
The 1 is smaller, so you output that to the new file and move on to 2.
2 is smaller than 4, so you output that and move on to 3.
3 is smaller than 4, so you output that and move on to 5.
4 is smaller than 5, so you output that and move on to 6.
5 is smaller than 6, so you output that and then you've reached the end of A.
Now just output the rest of B: 6, 8, 9.
This gives you [1,2,3,4,5,6,8,9].