Java - METODOS DE BUSQUEDA INTERNA Y EXTERNA

 
Vista:
sin imagen de perfil
Val: 19
Ha disminuido su posición en 3 puestos en Java (en relación al último mes)
Gráfica de Java

METODOS DE BUSQUEDA INTERNA Y EXTERNA

Publicado por Gabriel Francisco (11 intervenciones) el 22/06/2019 19:20:20
DISCULPEN ALGUIEN ME PODRIA AYUDAR A COMPLEMENTAR MI CLASE, LE FALTA UN METODO DE INTERCALACIÓN EN TIPO ENTERO, YA QUE NO SE COMO AGREGARLO...

LES DEJO MI CÓDIGO MAS ABAJO ESPERO ME PUEDAN AYUDAR !


CODIGO:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package Proyecto_3;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Proyecto {
 
    public int arreglo1[];
    private int arreglo2[];
    int x, y;
    // String archivo ;
 
    RandomAccessFile archivo = null; //aleatorio
    RandomAccessFile archivos = null;
    // constructor con el nombre del archivo
 
    public Proyecto(int x, int y) {
 
        try {
            archivo = new RandomAccessFile("Desordenado.bin", "rw");
            this.x = x;
            this.y = y;
        } catch (FileNotFoundException ex) {
 
        }
    }
 
    // ingresando las dimenciones del arreglo
    public void IngresarTam() {
 
        arreglo1 = new int[x];
        arreglo2 = new int[y];
    }
 
    // ingresando valores aleatorios  a los arreglos  
    // Guardando en el arrchivo el arreglo
    public void ingresarDatos() {
 
        try {
            for (int i = 0; i < arreglo1.length; i++) {
 
                arreglo1[i] = (int) (Math.random() * 100 + 5);
                archivo.writeInt(arreglo1[i]); // escribimos en el archivo
            }
            for (int j = 0; j < arreglo2.length; j++) {
                arreglo2[j] = (int) (Math.random() * 200 + 10);
                archivo.writeInt(arreglo2[j]); // escribimos en el archivo
            }
        } catch (IOException e) {
 
            System.out.println("Error : " + e);
        } finally {
            try {
                // Nuevamente aprovechamos el finally para 
                // asegurarnos que se cierra el fichero.
                if (null != archivo) {
                    archivo.close(); // cerrar archivo 
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
 
    public void mostrarArreglo1() {
 
        System.out.println("\nArreglo1");
        for (int i = 0; i < arreglo1.length; i++) {
 
            System.out.print("[" + arreglo1[i] + "] ");
        }
    }
 
    public void mostrarArreglo2() {
 
        System.out.println("\nArreglo2");
        for (int j = 0; j < arreglo2.length; j++) {
 
            System.out.print("[" + arreglo2[j] + "] ");
        }
    }
 
    public void mostrarArchivo() {
 
        try {
            // archiv = new FileReader("C:\\Users\\Reyma\\Desktop\\POO2-ORTEGA\\Ficheros\\Proyecto.bin");
 
        } catch (Exception e) {
 
            System.out.println("Error: " + e);
        }
    }
 
    public void ShellArreglo1() {
 
        int salto, i, j, k, aux;
        int pasadas = 0, comparaciones = 0, cambios = 0;
        salto = arreglo1.length / 2;
        while (salto > 0) {
            pasadas++;
 
            for (i = salto; i < arreglo1.length; i++) {
                j = i - salto;
 
                while (j >= 0) {
                    comparaciones++;
                    k = j + salto;
 
                    if (arreglo1[j] <= arreglo1[k]) {
                        j = -1;
 
                    } else {
                        cambios++;
                        aux = arreglo1[j];
                        arreglo1[j] = arreglo1[k];
                        arreglo1[k] = aux;
                        j = j - salto;
                    }
                }
 
            }
            salto = salto / 2;
            System.out.println("\nPasada No, " + pasadas + " ");
            mostrarArreglo1();
            System.out.println("Ingrese el nobre: ");
        }
 
        System.out.println("\nArreglo Ordenado con Shell");
        mostrarArreglo1();
 
        System.out.println("\nPasadas: " + pasadas + "\tComparaciones: " + comparaciones + "\tCambios: " + cambios);
    }
 
    public void ShellArreglo2() {
 
        int salto, i, j, k, aux;
        int pasadas = 0, comparaciones = 0, cambios = 0;
        salto = arreglo2.length / 2;
        while (salto > 0) {
            pasadas++;
 
            for (i = salto; i < arreglo2.length; i++) {
                j = i - salto;
 
                while (j >= 0) {
                    comparaciones++;
                    k = j + salto;
 
                    if (arreglo2[j] <= arreglo2[k]) {
                        j = -1;
 
                    } else {
                        cambios++;
                        aux = arreglo2[j];
                        arreglo2[j] = arreglo2[k];
                        arreglo2[k] = aux;
                        j = j - salto;
                    }
                }
 
            }
            salto = salto / 2;
            System.out.println("\nPasada No, " + pasadas + " ");
            mostrarArreglo2();
        }
 
        System.out.println("\nArreglo Ordenado con Shell");
        mostrarArreglo2();
 
        System.out.println("\nPasadas: " + pasadas + "\tComparaciones: " + comparaciones + "\tCambios: " + cambios);
    }
 
    private void Intercalacion(){
        int i,j,k;
        int Arreglo3[]=new int[arreglo1.length + arreglo2.length];
 
        //Repetir mientras los Arreglos A y B tengan elementos que comparar
        for(i = j = k = 0;i<arreglo1.length && j < arreglo2.length; k++){
            if(arreglo1[i] < arreglo2[j]){
                Arreglo3[k]=arreglo1[i];
                i++;
            } else {
                Arreglo3[k]=arreglo2[j];
                j++;
            }
        }
        //Para añadir a Arreglo C los elementos del Arreglo A sobrantes en caso de haberlo
        for(;i<arreglo1.length; j++, k++){
            Arreglo3[k]=arreglo1[i];
        }
        //Para añadir a Arreglo C los elementos del Arreglo A sobrantes en caso de haberlo
        for(;j<arreglo2.length; j++, k++){
            Arreglo3[k]=arreglo2[j];
        }
        System.out.println("Arreglos ordenados por intercalacion");
        mostrarArreglo1();
    }
 
    public static void main(String[] args) {
 
        Scanner entrada = new Scanner(System.in);
        int x = 0, y = 0;
        int opcion;
 
        Proyecto obj = null;
 
        do {
 
            System.out.print("\n\nElige una opción"
                    + "\n1.- Ingresar Tamaño"
                    + "\n2.- Generar datos Aleatorios para el arreglo 1 y 2"
                    + "\n3.-Mostrar Arreglos 1"
                    + "\n4.-Mostrar arreglo 2"
                    + "\n5.-Mostrar archivo"
                    + "\n6.- Ordenacion del Arreglo 1"
                    + "\n7.- Ordenacion del Arreglo 2"
                    + "\n8.- Ordenación por Intercalación"
                    + "\nOpcion: ");
            opcion = entrada.nextInt();
 
            switch (opcion) {
                case 1:
 
                    if (x == 0 || y == 0) {
 
                        System.out.print("\nIngrese Tamaño del Arreglo 1: ");
                        x = entrada.nextInt();
                        System.out.print("Ingrese Tamaño del Arreglo 2: ");
                        y = entrada.nextInt();
                        obj = new Proyecto(x, y);
                        obj.IngresarTam();
                    } else {
 
                    }
 
                    break;
                case 2:
                    obj.ingresarDatos();
                    break;
                case 3:
                    obj.mostrarArreglo1();
                    break;
                case 4:
                    obj.mostrarArreglo2();
                    //  obj.ShellArreglo1();
                    //obj.ShellArreglo2();
                    break;
                case 5:
                    obj.mostrarArchivo();
                    //   obj.Intercalacion();
                    break;
                case 6:
                    obj.ShellArreglo1();
                    break;
                case 7:
                    obj.ShellArreglo2();
                    break;
                case 8:
                    obj.Intercalacion();
                    break;
            }
        } while (opcion != 0);
    }
}
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
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

METODOS DE BUSQUEDA INTERNA Y EXTERNA

Publicado por Billy Joel (876 intervenciones) el 26/06/2019 18:43:45
A ver si entiendo,
tienes el arreglo1 = {1,2,3,4};
tienes el arreglo2 = {9,8,7,6};
¿El resultado del arreglo intercalado (Arreglo3) debería sería esto: {1,9,2,8,3,7,4,6};?

??
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