Filtro CPL Polarizado ventajas

Métodos de ordenación en Java (Intercambio, Inserción y Selección) (By Ivan Luis Jimenez- IvanovicHacker-Ivanovich)

En este programa agrupe los tres métodos señalados en el titulo, cada uno en un método diferente, funciona de la siguiente manera; primero en un  método lee un arreglo de enteros (en este caso 4 datos) y los almacena, otro método que muestra el menú con las tres opciones de ordenamiento, y otro método que muestra el arreglo ordenado.
Los tres métodos están en una clase diferente, es decir manejo dos clases, y en la otra manejo los métodos restantes como, leer los datos, mostrar los datos y en menú.

Bueno el código es el siguiente:


//ESTO EN UNA CLASE
package metodo_ordenacion;

/**
 *
 * @author Ivan Luis Jimenez
 */
import java.util.Scanner;

public class Metodo_Ordenacion {


    public void M_Intercambio(int[] arreglo) {
        System.out.println("\n==================================");
        System.out.println("Ordenacion por Metodo Intercambio");
        System.out.println("==================================\n");
        {
            int temp;
            for (int i = 1; i < arreglo.length; i++) {
                for (int j = 0; j < arreglo.length - 1; j++) {
                    if (arreglo[j] > arreglo[j + 1]) {
                        temp = arreglo[j];
                        arreglo[j] = arreglo[j + 1];
                        arreglo[j + 1] = temp;
                    }
                }
            }
        }

    }

    public void M_Inserccion(int[] arreglo) {
        System.out.println("\n==================================");
        System.out.println("Ordenacion por Metodo Inserccion");
        System.out.println("==================================\n");

        for (int i = 1; i < arreglo.length; i++) {
            int aux = arreglo[i];
            int j;
            for (j = i - 1; j >= 0 && arreglo[j] > aux; j--) {
                arreglo[j + 1] = arreglo[j];
            }
            arreglo[j + 1] = aux;
        }
    }

    public void M_Seleccion(int[] arreglo) {
        System.out.println("\n==================================");
        System.out.println("Ordenacion por Metodo Seleccion\n");
        System.out.println("==================================\n");
        int i, j, k, p, buffer, limit = arreglo.length - 1;
        for (k = 0; k < limit; k++) {
            p = k;
            for (i = k + 1; i <= limit; i++) {
                if (arreglo[i] < arreglo[p]) {
                    p = i;
                }
                if (p != k) {
                    buffer = arreglo[p];
                    arreglo[p] = arreglo[k];
                    arreglo[k] = buffer;
                }
            }
        }
    }

}



//ESTO EN OTRA CLASE EN DONDE IRA EL MÉTODO PRINCIPAL (MAIN)

package metodo_ordenacion;

import java.util.Scanner;

public class Principal {


    public static void main(String[] args) {
    Principal p = new Principal();
    int []arreglo=new int[4];
   
    p.Leer(arreglo);
    p.Mostrar(arreglo);
    p.Menu(arreglo);
    p.Mostrar(arreglo);

    }
   
   
    public void Leer(int []arreglo) {
        Scanner entrada = new Scanner(System.in);
     
        System.out.println("INGRESE LOS DATOS DEL ARREGLO:\n>");
        for (int a = 0; a < arreglo.length; a++) {
            System.out.println("Dato " + a);
            arreglo[a] = entrada.nextInt();
        }
    }

    public void Mostrar(int[] arreglo) {
        System.out.println("\n=====Datos del Arreglo===== ");
        for (int x = 0; x < arreglo.length; x++) {
            System.out.print("["+arreglo[x]+"]");
        }
        System.out.println("\n");
    }
   
        public void Menu(int[] arreglo){
        Scanner entrada = new Scanner(System.in);
        Metodo_Ordenacion call=new Metodo_Ordenacion();
        System.out.println("SELECCIONE EL METODO DE ORDENACION\n");
        System.out.println("1)Metodo Intercambio");
        System.out.println("2)Metodo Insercción");
        System.out.println("3)Metodo Selección\n>");
        int z=entrada.nextInt();
       
        if(z==1){
            call.M_Intercambio(arreglo);
        }
        else if (z==2){
            call.M_Inserccion(arreglo);
        }
        else{
            call.M_Seleccion(arreglo);
        }
    }
   

}


______________________________________________________________________________
EL CÓDIGO AQUÍ ESCRITO FUE IMPLEMENTADO POR IVAN LUIS JIMENEZ, ÚNICAMENTE LOS TRES MÉTODOS FUERON TOMADOS DE OTRAS FUENTES.

ESPERO Y  LES SEA DE MUCHA AYUDA!!!
IVANOVICHACKER----IVANOVICH


Comentarios