I am trying to preform an external insertion sort on a binary file full of random doubles between 0 and 1. I added a bunch of println statements after "value" and "temp" are assigned and it looks like they are getting the same value each iteration. I don't think I am traversing the file correctly.
public class ExternalFileSort
{
public static void sort(String filename, int length) throws IOException
{
int i, j;
double value, temp;
RandomAccessFile file = new RandomAccessFile(filename, "rw");
for (i = 1; i < length; i++)
{
file.seek(i);
temp = file.readDouble();
j = i-1;
file.seek(j);
value = file.readDouble();
while (j >= 0 && value > temp)
{
file.seek(j+1);
file.writeDouble(value);
j--;
}
file.seek(j+1);
file.writeDouble(temp);
}
file.close();
}
}
Assuming you have doubles written in your file according to that definition (that is a mandatory prerequisite) https://docs.oracle.com/javase/7/docs/api/java/io/DataOutput.html#writeDouble%28double%29
Not tested, but you get the idea.