diff --git a/diagram.png b/diagram.png index ded5b98..e5b6b5f 100644 Binary files a/diagram.png and b/diagram.png differ diff --git a/diagrama.uml b/diagrama.uml index 9642e47..a196c45 100644 --- a/diagrama.uml +++ b/diagrama.uml @@ -3,80 +3,49 @@ JAVA - cl.cromer.estructuras.InsercionController - cl.cromer.estructuras.BurbujaController - cl.cromer.estructuras.SeleccionController - cl.cromer.estructuras.PilaController - cl.cromer.estructuras.Logs - cl.cromer.estructuras.Array - cl.cromer.estructuras.ColaController - cl.cromer.estructuras.Pila - cl.cromer.estructuras.Colores - cl.cromer.estructuras.Grafico - cl.cromer.estructuras.TextFieldLimited - cl.cromer.estructuras.ArrayController - cl.cromer.estructuras.TextFieldLimited.StyleableProperties - cl.cromer.estructuras.Main - cl.cromer.estructuras.Cola - cl.cromer.estructuras.MenuController + cl.cromer.estructuras.InsercionController + cl.cromer.estructuras.ShellController + cl.cromer.estructuras.BurbujaController + cl.cromer.estructuras.QuickController + cl.cromer.estructuras.PilaController + cl.cromer.estructuras.SeleccionController + cl.cromer.estructuras.Logs + cl.cromer.estructuras.Array + cl.cromer.estructuras.ColaController + cl.cromer.estructuras.Pila + cl.cromer.estructuras.Colores + cl.cromer.estructuras.Grafico + cl.cromer.estructuras.Array.ParticionarResult + cl.cromer.estructuras.TextFieldLimited + cl.cromer.estructuras.ArrayController + cl.cromer.estructuras.TextFieldLimited.StyleableProperties + cl.cromer.estructuras.Main + cl.cromer.estructuras.Cola + cl.cromer.estructuras.MenuController - - - - - - - - - - - - - - - - - - - - - - + + + + - - - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + @@ -84,78 +53,154 @@ - - - + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + - - + + - - - + + + + + + + + + - - - - + + + + - - - + + + - - - - - - - - - - - - - - + + - - - + + + - - - - - - - + - cl.cromer.estructuras.SeleccionController + cl.cromer.estructuras.InsercionController Fields diff --git a/src/cl/cromer/estructuras/Array.java b/src/cl/cromer/estructuras/Array.java index 3cd34a1..1261077 100644 --- a/src/cl/cromer/estructuras/Array.java +++ b/src/cl/cromer/estructuras/Array.java @@ -22,10 +22,10 @@ public class Array { /** * Crear el array con el tamaño pasador por argumento. - * @param temano int: El temaño del array a crear. + * @param tamano int: El tamaño del array a crear. */ - public Array(int temano) { - this.array = new String[temano]; + public Array(int tamano) { + this.array = new String[tamano]; size = 0; ordered = false; } @@ -367,4 +367,97 @@ public class Array { return punteroIzquerda; } } + + /** + * Ordenar el array usando merge. + * @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 merge(boolean paso) { + String array[] = this.array.clone(); + boolean cambio = false; + boolean cambio2; + String[] temp = new String[size()]; + cambio2 = recurenciaMerge(temp, 0, size() - 1, paso); + for (int i = 0; i < size(); i++) { + if (!array[i].equals(this.array[i])) { + cambio = true; + } + } + return (cambio || cambio2); + } + + /** + * El metodo recursivo para ordenar con merge. + * @param temp String[]: El array temporario para trabajar. + * @param izquerda int: El lado izquerda. + * @param derecha int: El lado derecha. + * @param paso boolean: Verdad si es paso por paso. + * @return boolean: Devolver si algo cambió. + */ + private boolean recurenciaMerge(String[] temp, int izquerda, int derecha, boolean paso) { + if (izquerda != derecha) { + boolean cambio; + boolean cambio2; + boolean cambio3; + + int medio = (izquerda + derecha) / 2; + cambio = recurenciaMerge(temp, izquerda, medio, paso); + if (paso && cambio) { + return true; + } + cambio2 = recurenciaMerge(temp, medio + 1, derecha, paso); + if (paso && cambio2) { + return true; + } + cambio3 = merge(temp, izquerda, medio + 1, derecha, paso); + return (paso && cambio3); + } + else { + return false; + } + } + + /** + * Este metodo hace los cambios al array. + * @param temp String[]: El array temporario para trabajar. + * @param prevIzquerda int: El valor previo de la izquerda. + * @param prevMedio int: El valor previo al medio. + * @param prevDerecha int: El valor previo de la derecha. + * @param paso boolean: Si es paso por paso. + * @return boolean: Devolver si algo cambió. + */ + private boolean merge(String[] temp, int prevIzquerda, int prevMedio, int prevDerecha, boolean paso) { + boolean cambio = false; + int j = 0; + int izquerda = prevIzquerda; + int medio = prevMedio - 1; + int derecha = prevDerecha - izquerda + 1; + + while (prevIzquerda <= medio && prevMedio <= prevDerecha) { + if (Integer.valueOf(array[prevIzquerda]) < Integer.valueOf(array[prevMedio])) { + temp[j++] = array[prevIzquerda++]; + } + else { + temp[j++] = array[prevMedio++]; + } + } + + while (prevIzquerda <= medio) { + temp[j++] = array[prevIzquerda++]; + } + + while (prevMedio <= prevDerecha) { + temp[j++] = array[prevMedio++]; + } + + for (j = 0; j < derecha; j++) { + String temp2 = array[izquerda + j]; + array[izquerda + j] = temp[j]; + if (paso && !array[izquerda + j].equals(temp2)) { + cambio = true; + } + } + return cambio; + } } diff --git a/src/cl/cromer/estructuras/ArrayController.java b/src/cl/cromer/estructuras/ArrayController.java index 29a7d89..80c9143 100644 --- a/src/cl/cromer/estructuras/ArrayController.java +++ b/src/cl/cromer/estructuras/ArrayController.java @@ -259,7 +259,7 @@ public class ArrayController implements Initializable { } /** - * Crear el array de temaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. + * Crear el array de tamaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. */ private void initializeArray() { scene = contenidoArray.getScene(); diff --git a/src/cl/cromer/estructuras/BurbujaController.java b/src/cl/cromer/estructuras/BurbujaController.java index 13b79e5..74fac30 100644 --- a/src/cl/cromer/estructuras/BurbujaController.java +++ b/src/cl/cromer/estructuras/BurbujaController.java @@ -156,7 +156,7 @@ public class BurbujaController implements Initializable { } /** - * Crear el array de temaño 10. + * Crear el array de tamaño 10. */ private void initializeScene() { scene = contenidoBurbuja.getScene(); diff --git a/src/cl/cromer/estructuras/InsercionController.java b/src/cl/cromer/estructuras/InsercionController.java index 076b9d8..39d6d99 100644 --- a/src/cl/cromer/estructuras/InsercionController.java +++ b/src/cl/cromer/estructuras/InsercionController.java @@ -156,7 +156,7 @@ public class InsercionController implements Initializable { } /** - * Crear el array de temaño 10. + * Crear el array de tamaño 10. */ private void initializeScene() { scene = contenidoInsercion.getScene(); diff --git a/src/cl/cromer/estructuras/MenuController.java b/src/cl/cromer/estructuras/MenuController.java index 2f31a97..f553606 100644 --- a/src/cl/cromer/estructuras/MenuController.java +++ b/src/cl/cromer/estructuras/MenuController.java @@ -107,7 +107,7 @@ public class MenuController extends VBox implements Initializable { } /** - * Click en Selecion. + * Click en Shell. */ @FXML protected void menuShell() { @@ -119,14 +119,26 @@ public class MenuController extends VBox implements Initializable { } /** - * Click en Selecion. + * Click en Quick. */ @FXML protected void menuQuick() { loadStage( - resourceBundle.getString("tituloQuick"), - "/cl/cromer/estructuras/fxml/quick.fxml", - "/cl/cromer/estructuras/css/style.css" + resourceBundle.getString("tituloQuick"), + "/cl/cromer/estructuras/fxml/quick.fxml", + "/cl/cromer/estructuras/css/style.css" + ); + } + + /** + * Click en Merge. + */ + @FXML + protected void menuMerge() { + loadStage( + resourceBundle.getString("tituloMerge"), + "/cl/cromer/estructuras/fxml/merge.fxml", + "/cl/cromer/estructuras/css/style.css" ); } diff --git a/src/cl/cromer/estructuras/MergeController.java b/src/cl/cromer/estructuras/MergeController.java new file mode 100644 index 0000000..7ae2076 --- /dev/null +++ b/src/cl/cromer/estructuras/MergeController.java @@ -0,0 +1,174 @@ +package cl.cromer.estructuras; + +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.Scene; +import javafx.scene.control.ButtonBar; +import javafx.scene.control.ButtonType; +import javafx.scene.control.Dialog; +import javafx.scene.layout.HBox; +import javafx.scene.text.Text; + +import java.net.URL; +import java.util.Random; +import java.util.ResourceBundle; +import java.util.Scanner; + +/** + * Esta clase es para controlar todos la interfaz de Merge. + * @author Chris Cromer + */ +public class MergeController implements Initializable { + /** + * Donde poner el contenido de array. + */ + @FXML private HBox contenidoMerge; + + /** + * Donde va el codigo a mostrar a la pantalla. + */ + @FXML private Text codigoMerge; + + /** + * La escena donde está cosas graficas. + */ + private Scene scene; + + /** + * Donde está guardado los idiomas. + */ + private ResourceBundle resourceBundle; + + /** + * El array usado en la aplicación. + */ + private Array array; + + /** + * Inicializar todos los datos y dibujar las graficas. + * @param location URL: El URL de fxml en uso. + * @param resourceBundle ResourceBundle: Tiene datos de idioma. + */ + @Override + public void initialize(URL location, ResourceBundle resourceBundle) { + this.resourceBundle = resourceBundle; + + scene = null; + + Colores colores = new Colores(); + + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; + + array = new Array(10); + array.setOrdered(true); + + for (int i = 0; i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + while (array.buscar(numero) != -1) { + numero = random.nextInt(rango) + minimo; + } + array.insertar(numero); + contenidoMerge.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + colores.siguinteColor(); + } + } + + /** + * Crear un array nuevo. + */ + @FXML + protected void botonNuevo() { + if (scene == null) { + initializeScene(); + } + + array = new Array(10); + array.setOrdered(true); + + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; + + for (int i = array.size(); i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + while (array.buscar(numero) != -1) { + numero = random.nextInt(rango) + minimo; + } + array.insertar(numero); + } + generarGrafico(); + } + + /** + * Ordenarlo paso por paso. + */ + @FXML + protected void botonPaso() { + if (scene == null) { + initializeScene(); + } + + // Mostrar el codigo + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/merge/ordenar")).useDelimiter("\\Z").next(); + codigoMerge.setText(codigoTexto); + + if (!array.merge(true)) { + errorYaOrdenado(); + } + + generarGrafico(); + } + + /** + * Ordenarlo completamente. + */ + @FXML + protected void botonCorrer() { + if (scene == null) { + initializeScene(); + } + + // Mostrar el codigo + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/merge/ordenar")).useDelimiter("\\Z").next(); + codigoMerge.setText(codigoTexto); + + if (!array.merge(false)) { + errorYaOrdenado(); + } + + generarGrafico(); + } + + /** + * Se muestra un error si el array ya está ordenado. + */ + private void errorYaOrdenado() { + ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); + Dialog dialog = new Dialog<>(); + dialog.setTitle(resourceBundle.getString("error")); + dialog.setContentText(resourceBundle.getString("mergeYaOrdenado")); + dialog.getDialogPane().getButtonTypes().add(botonCerrar); + dialog.show(); + } + + /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoMerge.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#caja_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } +} diff --git a/src/cl/cromer/estructuras/QuickController.java b/src/cl/cromer/estructuras/QuickController.java index 363b588..418e10c 100644 --- a/src/cl/cromer/estructuras/QuickController.java +++ b/src/cl/cromer/estructuras/QuickController.java @@ -156,7 +156,7 @@ public class QuickController implements Initializable { } /** - * Crear el array de temaño 10. + * Crear el array de tamaño 10. */ private void initializeScene() { scene = contenidoQuick.getScene(); diff --git a/src/cl/cromer/estructuras/SeleccionController.java b/src/cl/cromer/estructuras/SeleccionController.java index a4bf19b..d6b0898 100644 --- a/src/cl/cromer/estructuras/SeleccionController.java +++ b/src/cl/cromer/estructuras/SeleccionController.java @@ -156,7 +156,7 @@ public class SeleccionController implements Initializable { } /** - * Crear el array de temaño 10. + * Crear el array de tamaño 10. */ private void initializeScene() { scene = contenidoSeleccion.getScene(); diff --git a/src/cl/cromer/estructuras/ShellController.java b/src/cl/cromer/estructuras/ShellController.java index cea3d2c..0ebf551 100644 --- a/src/cl/cromer/estructuras/ShellController.java +++ b/src/cl/cromer/estructuras/ShellController.java @@ -156,7 +156,7 @@ public class ShellController implements Initializable { } /** - * Crear el array de temaño 10. + * Crear el array de tamaño 10. */ private void initializeScene() { scene = contenidoShell.getScene(); diff --git a/src/cl/cromer/estructuras/bundles/Idioma_en.properties b/src/cl/cromer/estructuras/bundles/Idioma_en.properties index 7d6fdf5..af6a38d 100644 --- a/src/cl/cromer/estructuras/bundles/Idioma_en.properties +++ b/src/cl/cromer/estructuras/bundles/Idioma_en.properties @@ -8,6 +8,7 @@ tituloInsercion=Insertion tituloSeleccion=Selection tituloShell=Shell tituloQuick=Quick +tituloMerge=Merge estructuras=Structures array=Array @@ -19,6 +20,7 @@ insercion=Insertion seleccion=Selection shell=Shell quick=Quick +merge=merge listaEnlazada=Linked List listaSimple=Simple listaCircular=Circular @@ -81,6 +83,8 @@ shellYaOrdenado=The array is already sorted. quickYaOrdenado=The array is already sorted. +mergeYaOrdenado=The array is already sorted. + pilaLlena=Value not inserted because the stack is full. pilaVacia=The stack is empty. pilaNoValor=Please input a numeric value. diff --git a/src/cl/cromer/estructuras/bundles/Idioma_es.properties b/src/cl/cromer/estructuras/bundles/Idioma_es.properties index d312098..2e30387 100644 --- a/src/cl/cromer/estructuras/bundles/Idioma_es.properties +++ b/src/cl/cromer/estructuras/bundles/Idioma_es.properties @@ -8,6 +8,7 @@ tituloInsercion=Inserci\u00F3n tituloSeleccion=Selecci\u00F3n tituloShell=Shell tituloQuick=Quick +tituloMerge=Merge estructuras=Estructuras array=Array @@ -19,6 +20,7 @@ insercion=Inserci\u00F3n seleccion=Seleci\u00F3n shell=Shell quick=Quick +merge=Merge listaEnlazada=Lista Enlazada listaSimple=Simple listaCircular=Circular @@ -81,6 +83,8 @@ shellYaOrdenado=El array ya est\u00E1 ordenado. quickYaOrdenado=El array ya est\u00E1 ordenado. +mergeYaOrdenado=El array ya est\u00E1 ordenado. + pilaLlena=Valor no fue insertado porque la pila est\u00E1 llena. pilaVacia=La pila est\u00E1 vac\u00EDa. pilaNoValor=Ingresar un valor num\u00E9rico por favor. diff --git a/src/cl/cromer/estructuras/code/merge/ordenar b/src/cl/cromer/estructuras/code/merge/ordenar new file mode 100644 index 0000000..7aa9c3c --- /dev/null +++ b/src/cl/cromer/estructuras/code/merge/ordenar @@ -0,0 +1,50 @@ +public void merge() { + // Crear un array temporario para trabajar. + int[] temp = new int[elementos]; + // Llamar al metodo recursivo. + recMergeSort(temp, 0, elementos - 1); +} + +private void recMergeSort(int[] temp, int izquerda, int derecha) { + if (izquerda != derecha) { + int medio = (izquerda + derecha) / 2; + // Trabajar con los valores en el lado izquerdo. + recMergeSort(temp, izquerda, medio); + // Trabajar con los valores en el lado derecha. + recMergeSort(temp, medio + 1, derecha); + // Unir los valores. + merge(temp, izquerda, medio + 1, derecha); + } +} + +private void merge(int[] temp, int prevIzquerda, int prevMedio, int derecha) { + int j = 0; + int izquerda = prevIzquerda; + int medio = prevMedio - 1; + int masDerecha = derecha - izquerda + 1; + + while (prevIzquerda <= medio && prevMedio <= derecha) { + // Poner un valor en el array temporario. + if (array[prevIzquerda] < array[prevMedio]) { + temp[j++] = array[prevIzquerda++]; + } + else { + temp[j++] = array[prevMedio++]; + } + } + + while (prevIzquerda <= medio) { + // Mientras que el valor previos de izquerda es menor que el medio correr los valores. + temp[j++] = array[prevIzquerda++]; + } + + while (prevMedio <= derecha) { + // Mientras que el valor previos de derecha es menor que el medio correr los valores. + temp[j++] = array[prevMedio++]; + } + + for (j = 0; j < masDerecha; j++) { + // Copiar los valores al array real. + array[izquerda + j] = temp[j]; + } +} \ No newline at end of file diff --git a/src/cl/cromer/estructuras/fxml/menu.fxml b/src/cl/cromer/estructuras/fxml/menu.fxml index e784f74..bc182f9 100644 --- a/src/cl/cromer/estructuras/fxml/menu.fxml +++ b/src/cl/cromer/estructuras/fxml/menu.fxml @@ -14,6 +14,7 @@ + diff --git a/src/cl/cromer/estructuras/fxml/merge.fxml b/src/cl/cromer/estructuras/fxml/merge.fxml new file mode 100644 index 0000000..c8478ed --- /dev/null +++ b/src/cl/cromer/estructuras/fxml/merge.fxml @@ -0,0 +1,24 @@ + + + + + + + + + + + + +