pablosotol

Ejercicio 1 (Arreglo)

Mar 30th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package ejercicio1;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Arreglo{
  6.        int Arre[]=new int[100];
  7.        
  8.        public void generarNumeros(){
  9.               Random r1=new Random();
  10.               for(int i=0;i<Arre.length;i++){
  11.                   Arre[i]=r1.nextInt(9)+1;
  12.                  
  13.               }
  14.               mostrarNumeros(Arre);
  15.        }
  16.        
  17.        public void mostrarNumeros(int A[]){
  18.               for(int i=0;i<A.length;i++){
  19.                   System.out.println("Elemento" +i +":" +Arre[i]);              
  20.               }
  21.               seleccion();
  22.        }
  23.        
  24.        public void seleccion(){
  25.               Scanner ent=new Scanner(System.in);
  26.               int opc=0;
  27.               System.out.println("Ingrese 1 si desea ordenar de menor a mayor o ingrese 2 de mayor a menor: ");
  28.               opc=ent.nextInt();
  29.               if(opc==1){
  30.                  ordenarMenorMayor(Arre);
  31.               }
  32.               if(opc==2){
  33.                   ordenarMayorMenor(Arre);
  34.               }
  35.              
  36.        }
  37.        
  38.        public void ordenarMenorMayor(int A[]){
  39.               int aux;
  40.               for(int i=0;i<A.length-1;i++){
  41.                   for(int j=0;j<A.length-i-1;j++){
  42.                       if(A[j]>A[j+1]){
  43.                          aux=A[j];
  44.                          A[j]=A[j+1];                        
  45.                         A[j+1]=aux;
  46.                   }
  47.               }
  48.              
  49.        }
  50.               System.out.println("\nOrdenado de menor a mayor: ");
  51.               for(int i=0;i<A.length;i++){
  52.                   System.out.println();
  53.                   System.out.println("Elemento" +i +":" +Arre[i]);              
  54.               }
  55.              
  56.        }
  57.    
  58.          public void ordenarMayorMenor(int A[]){
  59.                 int aux;
  60.                 for(int i=0;i<A.length-1;i++){
  61.             for (int j=0;j<A.length-i-1;j++){
  62.                 if(A[j]<A[j+1]) {
  63.                     aux=A[j];
  64.                     A[j]=A[j+1];
  65.                     A[j+1]=aux;
  66.                 }
  67.             }
  68.         }
  69.                 System.out.println("\nOrdenado de mayor a menor: ");
  70.                  for(int i=0;i<A.length;i++){
  71.                   System.out.println();
  72.                   System.out.println("Elemento" +i +":" +A[i]);              
  73.               }
  74.          }
  75. }
Add Comment
Please, Sign In to add comment