50 millones no es particularmente grande. Solo los leería en la memoria. Ordenarlos y escribirlos. Debería tomar solo unos segundos. ¿Qué tan rápido lo necesitas? ¿Cuán compilado es necesario que sea?
En mi viejo laboratorio, tomó 28 segundos. Si tuviera más procesadores, podría ser un poco más rápido, pero pasaría la mayor parte del tiempo leyendo y escribiendo el archivo (15 segundos), que no sería más rápido.
Uno de los factores críticos es el tamaño de su caché. La comparación en sí es muy económica siempre que los datos estén en caché. Como la caché L3 se comparte, un hilo es todo lo que necesita para hacer un uso completo de la misma.
public static void main(String...args) throws IOException {
generateFile();
long start = System.currentTimeMillis();
int[] nums = readFile("numbers.bin");
Arrays.sort(nums);
writeFile("numbers2.bin", nums);
long time = System.currentTimeMillis() - start;
System.out.println("Took "+time+" secs to sort "+nums.length+" numbers.");
}
private static void generateFile() throws IOException {
Random rand = new Random();
int[] ints = new int[50*1000*1000];
for(int i= 0;i<ints.length;i++)
ints[i] = rand.nextInt();
writeFile("numbers.bin", ints);
}
private static int[] readFile(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename), 64*1024));
int len = dis.readInt();
int[] ints = new int[len];
for(int i=0;i<len;i++)
ints[i] = dis.readInt();
return ints;
}
private static void writeFile(String name, int[] numbers) throws IOException {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(name), 64*1024));
dos.writeInt(numbers.length);
for (int number : numbers)
dos.writeInt(number);
dos.close();
}
:) ¿Qué quieres decir? –
@Paul Él es solo de la Matriz - mira su apodo :) –
¿Por qué no puedes usar algoritmos estándar? ¿Es esto un problema de tarea? –