Tarea metodos de busqueda
Enviado por papuxxddx • 23 de Marzo de 2022 • Tarea • 1.299 Palabras (6 Páginas) • 52 Visitas
Método 1. Burbuja Mejorado.
Código.
package MetodosDeOrdenamiento;
class main{
public static void main(String[] args) {
int[]arreglo={63,76,28,57,57,93,69,71,69,68,50,90,43,41,25,75,60,75,2,73,35,65,7,33,15,69,73,76,5,28,89,30,86,
75,9,19,78,58,88,31,91,75,25,26,57,90,9,63,20,21,79,16,88,35,8,52,98,34,16,28,95,93,99,87,22,99,63,36,
67,39,67,82,7,42,69,52,52,52,78,3,21,94,42,77,59,96,59,96,31,19,58,28,49,69,64,41,34,2,52,50,42};
Ordenamiento metodo = new Ordenamiento(arreglo);
System.out.println("Arreglo original:");
metodo.imprimir();
metodo.ordenar();
System.out.println("Arreglo ordenado: ");
metodo.imprimir();
}
}
class Ordenamiento{
private int[] x;
private int n;
public Ordenamiento(int[] x) {
this.x = x;
n=this.x.length;
}
public void ordenar(){
int aux;
for(int i=0;i<n-2;i++){
for(int j=0;j<n-i-2;j++){
if(x[j]>x[j+1]){
aux=x[j];
x[j]=x[j+1];
x[j+1]=aux;
}
}
}
}
public void imprimir(){
for(int j=0; j <= n -1; j++){
System.out.print(" "+x[j]);
}
System.out.println();
}
}
Pantalla de salida.
[pic 1]
Método 2. De inserción.
Código:
package MetodosDeOrdenamiento;
class main {
public static void main(String[] args) {
int[] arreglo = {63, 76, 28, 57, 57, 93, 69, 71, 69, 68, 50, 90, 43, 41, 25, 75, 60, 75, 2, 73, 35, 65, 7, 33, 15, 69, 73, 76, 5, 28, 89, 30, 86,
75, 9, 19, 78, 58, 88, 31, 91, 75, 25, 26, 57, 90, 9, 63, 20, 21, 79, 16, 88, 35, 8, 52, 98, 34, 16, 28, 95, 93, 99, 87, 22, 99, 63, 36,
67, 39, 67, 82, 7, 42, 69, 52, 52, 52, 78, 3, 21, 94, 42, 77, 59, 96, 59, 96, 31, 19, 58, 28, 49, 69, 64, 41, 34, 2, 52, 50, 42};
Ordenamiento metodo = new Ordenamiento(arreglo);
System.out.println("Arreglo original:");
metodo.imprimir();
metodo.ordenar();
System.out.println("Arreglo ordenado: ");
metodo.imprimir();
}
}
class Ordenamiento {
private int[] x;
private int n;
public Ordenamiento(int[] x) {
this.x = x;
n = this.x.length;
}
public void ordenar() {
int aux;
int k;
boolean sw;
for (int i = 1; i < n - 1; i++) {
aux = x[i];
k = i - 1;
sw = false;
while (!(sw) && (k>= 0)){
if (aux < x[k]) {
x[k + 1] = x[k];
k = k - 1;
} else {
sw = true;
}
}
x[k + 1] = aux;
}
}
public void imprimir() {
for (int j = 0; j <= n - 1; j++) {
if(j%15==0) System.out.println();
System.out.print(" " + x[j]);
}
System.out.println();
}
}
...