Created
June 10, 2012 22:36
-
-
Save delucas/2907560 to your computer and use it in GitHub Desktop.
#untref #lp1 - Algoritmos de ordenamiento - Alumnos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuscadorBinario { | |
public int buscadorBinarioSimple (ArregloOrdenable arreglo, int buscado){ | |
int posicionDelValorBuscado = 0; | |
int posicionMenor = 0; | |
int posicionMayor = arreglo.getLargo()-1; | |
int promedio = calcularPromedio(posicionMenor, posicionMayor); | |
boolean encontrado = false; | |
while (!encontrado){ | |
if (buscado == arreglo.get(posicionMenor)){ | |
posicionDelValorBuscado = posicionMenor; | |
encontrado = true; | |
} | |
else if (buscado == arreglo.get(posicionMayor)){ | |
posicionDelValorBuscado = posicionMayor; | |
encontrado = true; | |
} | |
if(buscado == arreglo.get(promedio)){ | |
posicionDelValorBuscado = promedio; | |
encontrado = true; | |
} | |
else if(buscado < arreglo.get(this.calcularPromedio(posicionMenor, posicionMayor))){ | |
posicionMayor = this.calcularPromedio(posicionMenor, posicionMayor); | |
} | |
else { | |
posicionMenor = this.calcularPromedio(posicionMenor, posicionMayor); | |
} | |
} | |
return posicionDelValorBuscado; | |
} | |
private int calcularPromedio(int valor1, int valor2){ | |
return (valor1 + valor2)/2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BusquedaSecuencial { | |
private int buscado; | |
public int busqueda(ArregloOrdenable arreglo, int buscado) { | |
this.buscado = buscado; | |
for (int i = 0; i < arreglo.getLargo() | |
|| arreglo.get(i) == this.buscado; i++) { | |
if ((arreglo.get(i) == this.buscado) == true) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InsertionSort { | |
public void insertion_srt(int array[], int n){ | |
for (int i = 1; i < n; i++){ | |
int j = i; | |
int b = array[i]; | |
while ((j > 0) && (array[j-1] > b)){ | |
array[j] = array[j-1]; | |
j--; | |
} | |
array[j] = b; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Mezcla { | |
public int[] mezclarDosArreglos(int[] arreglo1, int[] arreglo2) { | |
int[] arregloPro = new int[arreglo1.length + arreglo2.length]; | |
// aca empieza a mezclarlos y meterlos en el arregloPro | |
// indice del arregloPro | |
int i = 0; | |
// indice del arreglo1 | |
int j = 0; | |
// indice del arreglo2 | |
int k = 0; | |
int largo1 = arreglo1.length; | |
int largo2 = arreglo2.length; | |
while (j < largo1 && k < largo2) { | |
if (arreglo1[j] < arreglo2[k]) { | |
// colocara el primer elemento del arreglo1 en la siguiente | |
// posicion del arregloPro | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
if (j == largo1) { | |
while (k < largo2) { | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
} | |
} | |
} else { // if(arreglo2[k] < arreglo1[j]) | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
if (k == largo2) { | |
while (j < largo1) { | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
} | |
} | |
} | |
} | |
return arregloPro; | |
} | |
public int[] mezclarTresArreglos(int[] arreglo1, int[] arreglo2, | |
int[] arreglo3) { | |
int[] arregloPro = new int[arreglo1.length + arreglo2.length | |
+ arreglo3.length]; | |
// aca empieza a mezclarlos y meterlos en el arregloPro | |
// indice del arregloPro | |
int i = 0; | |
// indice del arreglo1 | |
int j = 0; | |
// indice del arreglo2 | |
int k = 0; | |
// indice del arreglo3 | |
int l = 0; | |
int largo1 = arreglo1.length; | |
int largo2 = arreglo2.length; | |
int largo3 = arreglo3.length; | |
while (j < largo1 && k < largo2 && l < largo3) { | |
if (arreglo1[j] < arreglo2[k] && arreglo1[j] < arreglo3[l]) { | |
// colocara el primer elemento del arreglo1 en la siguiente | |
// posicion del arregloPro | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
if (j == largo1) { | |
while (k < largo2 && l < largo3) { | |
if (arreglo2[k] < arreglo3[l]) { | |
// colocara el primer elemento del arreglo1 en la | |
// siguiente | |
// posicion del arregloPro | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
if (k == largo2) { | |
while (l < largo3) { | |
arregloPro[i++] = arreglo3[l]; | |
l++; | |
} | |
} | |
} else { // if(arreglo3[l] < arreglo2[k]) | |
arregloPro[i++] = arreglo3[l]; | |
l++; | |
if (l == largo3) { | |
while (k < largo2) { | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
} | |
} | |
} | |
} | |
} | |
} else if (arreglo2[k] < arreglo1[j] && arreglo2[k] < arreglo3[l]) { // if(arreglo2[k] | |
// < | |
// arreglo1[j]) | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
if (k == largo2) { | |
while (j < largo1 && l < largo3) { | |
if (arreglo1[j] < arreglo3[l]) { | |
// colocara el primer elemento del arreglo1 en la | |
// siguiente | |
// posicion del arregloPro | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
if (j == largo1) { | |
while (l < largo3) { | |
arregloPro[i++] = arreglo3[l]; | |
l++; | |
} | |
} | |
} else { // if(arreglo3[l] < arreglo1[j]) | |
arregloPro[i++] = arreglo3[l]; | |
l++; | |
if (l == largo3) { | |
while (j < largo1) { | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
} | |
} | |
} | |
} | |
} | |
} else { // if(arreglo3[l] < arreglo1[j] && arreglo3[l] < | |
// arreglo2[k]) | |
arregloPro[i++] = arreglo3[l]; | |
l++; | |
if (l == largo3) { | |
while (j < largo1 && k < largo2) { | |
if (arreglo1[j] < arreglo2[k]) { | |
// colocara el primer elemento del arreglo1 en la | |
// siguiente | |
// posicion del arregloPro | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
if (j == largo1) { | |
while (k < largo2) { | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
} | |
} | |
} else { // if(arreglo2[k] < arreglo1[j]) | |
arregloPro[i++] = arreglo2[k]; | |
k++; | |
if (k == largo2) { | |
while (j < largo1) { | |
arregloPro[i++] = arreglo1[j]; | |
j++; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return arregloPro; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrdenamientoPorBurbujeo { | |
/* | |
* Algoritmo de metodo de ordenamiento por burbujeo (primera mejora). | |
*/ | |
public void ordenar(ArregloOrdenable arreglo){ | |
int limite = arreglo.getLargo(); | |
for(int i=0; i< limite; i++){ | |
for(int j=1; j< limite; j++){ | |
if(arreglo.get(j) < arreglo.get(j-1)){ | |
arreglo.intercambiar(j, j-1); | |
} | |
} | |
limite--;//sin esta linea es burbujeo original. | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrdenaminetoPorSeleccion { | |
ArregloOrdenable arregloOrdenable; | |
public OrdenaminetoPorSeleccion (int [] arreglo){ | |
this.arregloOrdenable = new ArregloOrdenable(arreglo); | |
} | |
public void ordenar ( ){ | |
int mayor = 0; | |
int posicion = 0 ; | |
int auxiliar; | |
for ( int i = 0 ; i < this.arregloOrdenable.getLargo() - 1 ; i++){ | |
mayor = this.arregloOrdenable.get(0); | |
posicion = 0; | |
for ( int j = 1 ; j < this.arregloOrdenable.getLargo() - i ; j++){ | |
if ( mayor < this.arregloOrdenable.get(j)){ | |
posicion = j; | |
mayor = this.arregloOrdenable.get(j); | |
} | |
} | |
this.arregloOrdenable.intercambiar(posicion, this.arregloOrdenable.getLargo() - i - 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment