diff --git a/src/cl/cromer/estructuras/Array.java b/src/cl/cromer/estructuras/Array.java index 569cf03..989fbc7 100644 --- a/src/cl/cromer/estructuras/Array.java +++ b/src/cl/cromer/estructuras/Array.java @@ -210,4 +210,40 @@ public class Array { return cambio; } + + /** + * Ordenar el array usando shell. + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean shell(boolean paso) { + boolean cambio = false; + + int j, i; + String temp; + + int h = 1; + while (h <= size() / 3) { + h = h * 3 + 1; + } + + while (h > 0) { + for (i=h; i < size(); i++) { + temp = array[i]; + j = i; + while (j > h-1 && Integer.valueOf(array[j-h]) >= Integer.valueOf(temp)) { + array[j] = array[j-h]; + j -= h; + cambio = true; + } + array[j] = temp; + if (paso && cambio) { + return true; + } + } + h = (h-1) / 3; + } + + return cambio; + } } diff --git a/src/cl/cromer/estructuras/ShellController.java b/src/cl/cromer/estructuras/ShellController.java index 2424370..cea3d2c 100644 --- a/src/cl/cromer/estructuras/ShellController.java +++ b/src/cl/cromer/estructuras/ShellController.java @@ -22,12 +22,12 @@ public class ShellController implements Initializable { /** * Donde poner el contenido de array. */ - @FXML private HBox contenidoSeleccion; + @FXML private HBox contenidoShell; /** * Donde va el codigo a mostrar a la pantalla. */ - @FXML private Text codigoSeleccion; + @FXML private Text codigoShell; /** * La escena donde está cosas graficas. @@ -71,7 +71,7 @@ public class ShellController implements Initializable { numero = random.nextInt(rango) + minimo; } array.insertar(numero); - contenidoSeleccion.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + contenidoShell.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); colores.siguinteColor(); } } @@ -113,10 +113,10 @@ public class ShellController implements Initializable { } // Mostrar el codigo - String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/seleccion/ordenar")).useDelimiter("\\Z").next(); - codigoSeleccion.setText(codigoTexto); + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/shell/ordenar")).useDelimiter("\\Z").next(); + codigoShell.setText(codigoTexto); - if (!array.seleccion(true)) { + if (!array.shell(true)) { errorYaOrdenado(); } @@ -133,10 +133,10 @@ public class ShellController implements Initializable { } // Mostrar el codigo - String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/seleccion/ordenar")).useDelimiter("\\Z").next(); - codigoSeleccion.setText(codigoTexto); + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/shell/ordenar")).useDelimiter("\\Z").next(); + codigoShell.setText(codigoTexto); - if (!array.seleccion(false)) { + if (!array.shell(false)) { errorYaOrdenado(); } @@ -150,7 +150,7 @@ public class ShellController implements Initializable { ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); Dialog dialog = new Dialog<>(); dialog.setTitle(resourceBundle.getString("error")); - dialog.setContentText(resourceBundle.getString("seleccionYaOrdenado")); + dialog.setContentText(resourceBundle.getString("shellYaOrdenado")); dialog.getDialogPane().getButtonTypes().add(botonCerrar); dialog.show(); } @@ -159,7 +159,7 @@ public class ShellController implements Initializable { * Crear el array de temaño 10. */ private void initializeScene() { - scene = contenidoSeleccion.getScene(); + scene = contenidoShell.getScene(); } /** diff --git a/src/cl/cromer/estructuras/bundles/Idioma_es.properties b/src/cl/cromer/estructuras/bundles/Idioma_es.properties index cee58b5..d853a44 100644 --- a/src/cl/cromer/estructuras/bundles/Idioma_es.properties +++ b/src/cl/cromer/estructuras/bundles/Idioma_es.properties @@ -6,7 +6,7 @@ tituloCola=Cola tituloBurbuja=Burbuja tituloInsercion=Inserci\u00F3n tituloSeleccion=Selecci\u00F3n -tutuloShell=Shell +tituloShell=Shell estructuras=Estructuras array=Array diff --git a/src/cl/cromer/estructuras/code/shell/ordenar b/src/cl/cromer/estructuras/code/shell/ordenar new file mode 100644 index 0000000..d573346 --- /dev/null +++ b/src/cl/cromer/estructuras/code/shell/ordenar @@ -0,0 +1,25 @@ +public void shell() { + int i, j; + int temp; + + int h = 1; + while (h <= elementos / 3) { + // Sumatorio de (h * 3 + 1) + h = h * 3 + 1; + } + + // Mientras que h es mayor que 0. + while (h > 0) { + for (i = h; i < elementos; i++) { + temp = array[i]; + j = i; + while (j > h-1 && array[j-h] >= temp) { + // Ordenar dento el "shell" + array[j] = array[j-h]; + j -= h; + } + array[j] = temp; + } + h = (h-1) / 3; + } +} \ No newline at end of file