Java - Ordenación Mergesort Iterativo/Recursivo

 
Vista:
Imágen de perfil de Nicolas

Ordenación Mergesort Iterativo/Recursivo

Publicado por Nicolas (3 intervenciones) el 06/11/2014 03:28:29
Hola, Buenas noches alguien me podría ayudar con este programa que ordena un archivo por nombre con el método de MergeSort a implementarlo de manera recursiva, Gracias de antemano.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package oad;
import java.io.*;
public class MergeSort {
 
public static void main(String[] args) throws Exception {
File orig = new File(Constantes.CONTACTOS);
File tmp1 = new File(Constantes.TEMPORAL);
File tmp2 = new File(Constantes.TEMPORAL2);
int numReg = (int) orig.length() / Constantes.LEN_RECORD;
int longSec= 1;
while (longSec < numReg) {
partir(orig, tmp1, tmp2, longSec, numReg);
mezclar(tmp1, tmp2, orig, longSec, numReg);
longSec *= 2;
}
}
 
private static void partir(File orig, File tmp1, File tmp2, int longSec, int numReg)
throws IOException
{
BufferedInputStream ori = new BufferedInputStream(new FileInputStream(orig));
BufferedOutputStream t1 = new BufferedOutputStream(new FileOutputStream(tmp1));
BufferedOutputStream t2 = new BufferedOutputStream(new FileOutputStream(tmp2));
int numSec = numReg / (2*longSec);
int resto = numReg % (2*longSec);
for (int i = 1; i <= numSec; i++) {
subSecuencia(ori, t1, longSec);
subSecuencia(ori, t2, longSec);
}
if (resto > longSec)
resto -= longSec;
else {
longSec = resto;
resto = 0;
}
subSecuencia(ori, t1, longSec);
subSecuencia(ori, t2, resto);
ori.close();
t1.close();
t2.close();
}
 
private static void subSecuencia( BufferedInputStream orig,
BufferedOutputStream tmp, int longSec)
throws IOException
{
byte [] registro = new byte [Constantes.LEN_RECORD];
for (int j = 1; j <= longSec; j++) {
orig.read(registro);
tmp.write(registro);
}
}
 
private static void mezclar ( File tmp1, File tmp2,
File orig, int longSec, int numReg)
throws IOException
{
int numSec = numReg / (2*longSec);
int resto = numReg % (2*longSec);
BufferedInputStream t1 = new BufferedInputStream(new FileInputStream(tmp1));
BufferedInputStream t2 = new BufferedInputStream(new FileInputStream(tmp2));
BufferedOutputStream ori= new BufferedOutputStream(new FileOutputStream(orig));
byte [] registro1 = new byte [Constantes.LEN_RECORD];
byte [] registro2 = new byte [Constantes.LEN_RECORD];
t1.read(registro1);
t2.read(registro2);
for (int s=1; s<=numSec+1; s++) {
String nombre1 = Funciones.arrByte2String(registro1,Constantes.LEN_NOMBRE);
String nombre2 = Funciones.arrByte2String(registro2,Constantes.LEN_NOMBRE);
int n1=longSec;
int n2=longSec;
if (s == numSec+1) {
if (resto > longSec)
n2 = resto - longSec;
else {
n1 = resto;
n2 = 0;
}
}
int i = 1;
int j = 1;
while (i <= n1 && j <= n2) {
if (nombre1.compareTo(nombre2)<0) {
ori.write(registro1);
t1.read(registro1);
nombre1 = Funciones.arrByte2String(registro1,Constantes.LEN_NOMBRE);
i++;
} else {
ori.write(registro2);
t2.read(registro2);
nombre2 = Funciones.arrByte2String(registro2,Constantes.LEN_NOMBRE);
j++;
}
}
for (int k=i; k<=n1; k++) {
ori.write(registro1);
t1.read(registro1);
}
for (int k=j; k<=n2; k++) {
ori.write(registro2);
t2.read(registro2);
}
}
ori.close();
t1.close();
t2.close();
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Ordenación Mergesort Iterativo/Recursivo

Publicado por Andrés (340 intervenciones) el 06/11/2014 10:15:38
Revisa:

http://stackoverflow.com/questions/20795158/sorting-names-using-merge-sort
http://www.vogella.com/tutorials/JavaAlgorithmsMergesort/article.html
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar