La descarga está en progreso. Por favor, espere

La descarga está en progreso. Por favor, espere

Algoritmo y Estructura de Datos I

Presentaciones similares


Presentación del tema: "Algoritmo y Estructura de Datos I"— Transcripción de la presentación:

1 Algoritmo y Estructura de Datos I
Facultad de Ingeniería y Arquitectura Algoritmo y Estructura de Datos I I Sesión 6 - Busqueda y Ordenamiento Juan José Montero Román.

2 Busqueda y Ordenamiento
Busqueda Secuencial public int buscar(int nota){ for(int i=0;i<n.length;i++){ if(n[i]==nota) return i; } return -1;

3 Busqueda y Ordenamiento Ordenamiento de Burbuja
Ordenamiento de un vector de números en forma ascendente; Donde n es un vector de enteros: int n = new int[5] public void ordenarNumAscendente ( ){ for(int i=0;i<tamaño -1 ;i++) for(int j=i+1;j<tamaño;j++) if ( n[i] > n[j] ) { int aux = n[i]; n[i] = n[j]; n[j] = aux; }

4 Busqueda y Ordenamiento Ordenamiento de Burbuja
Ordenamiento de un vector String en forma ascendente; Donde cad es un vector de cadenas: String cad = new String[5] public void ordenarCadenasAscendente ( ) { for(int i=0;i<tamaño -1;i++) for(int j=i+1;j<tamaño;j++) if ( cad[i].compareTo(cad[j]) > 0) { String aux = cad[i]; cad[i] = cad[j]; cad[j] = aux; }

5 Diseñe un programa que reciba 5 edades. Se pide lo siguiente:
Ejercicio Diseñe un programa que reciba 5 edades. Se pide lo siguiente: Buscar una edad ingresada Ordenar las edades en forma ascendente

6 package domapli; import biblioteca.*; public class PrgEdad { int edad[]=new int[7]; int suma=0; public void ingresar(){ for(int i=0;i<edad.length;i++){ edad[i]=LE.leerInt("Ingrese edad["+ i+"]"); } public void ordenar(){ for(int i=0;i<edad.length -1 ;i++) for(int j=i+1;j< edad.length;j++) if(edad[i]>edad[j]){ int aux=edad[i]; edad[i]=edad[j]; edad[j]=aux; public void mostrar(){ String msg; msg+=edad[i]+"\n"); LE.mostrarInformacion("Listado\n"+msg); public int buscar(int e){ if(edad[i]==e) return i; return -1; public void busqueda(){ int e=LE.leerInt("Ingrese edad a buscar"); int i=buscar(e); if(i==-1) LE.mostrarInformacion("No existe"); Else LE.mostrarInformacion("Edad encontrada"); public static void main(String args[]){ PrgEdad obj = new PrgEdad(); obj.ingresar(); obj.busqueda(); obj.ordenar(); obj.mostrar();

7 Ejercicio Diseñe un programa que reciba N notas, ordénelas en forma descendente y muestre la primera, la ultima y la nota media.

8 public void ingresar(){
package domApli; import biblioteca.*; public class PrgOrd02 { int nota[]; int suma=0; public void ingresar(){ int dimen=LE.leerInt("Ingresar dimencion del arreglo"); nota = new int[dimen]; for(int i=0;i<nota.length;i++){ nota[i]=LE.leerInt("Ingrese edad["+ i+"]"); } public void ordenar(){ for(int i=0;i<nota.length -1 ;i++) for(int j=i+1;j< nota.length;j++) if(nota[i]>nota[j]){ int aux=nota[i]; nota[i]=nota[j]; nota[j]=aux; public void mostrar(){ String msg=""; msg+=nota[i]+"\n"; LE.mostrarInformacion("Listado\n"+msg); public void mostrar2(){ if(i==0 || i==nota.length/2 || i==nota.length-1){ public static void main(String args[]){ PrgOrd02 obj = new PrgOrd02(); obj.ingresar(); obj.ordenar(); obj.mostrar(); obj.mostrar2();

9 Ejercicio Diseñe un programa que almacene los nombres de 5 alumnos, ordénelos por tamaño del nombre y muestre al nombre mas largo

10


Descargar ppt "Algoritmo y Estructura de Datos I"

Presentaciones similares


Anuncios Google