Advertisement
LeoSchimidt

bubble sort crescente ordenação

Aug 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package ordenacao1;
  2. import java.util.Scanner;
  3. /**
  4.  *
  5.  * @author alunoead
  6.  */
  7. public class Ordenacao1 {
  8.    
  9.     /**
  10.      * @param args the command line arguments
  11.      */
  12.     public static void main(String[] args) {
  13.         // TODO code application logic here
  14.        
  15.         int x[] = new int [5];
  16.         int n, i, aux;
  17.        
  18.         Scanner sc1 = new Scanner(System.in);
  19.        
  20.         //carregando os numeros no vetor
  21.         for(i = 0; i <= 4; i++)
  22.             {
  23.                 System.out.println("Digite o "+(i+1)+"° número: ");
  24.             x[i] = sc1.nextInt();
  25.             }  
  26.             //ordenando de forma crescente
  27.             //laço com quantidade de elementos do vetor
  28.            
  29.             for(n=1; n <= 5; n++)
  30.             {
  31.                 for(i = 0; i <= 3; i++)
  32.                 {
  33.                     if (x[i] > x[i+1])
  34.                         {aux = x[i];
  35.                         x[i] = x[i+1];
  36.                          x[i+1] = aux;
  37.                         }
  38.                 }
  39.             }
  40.            
  41.             //mostrando o vetor ordenado
  42.             for(i=0; i <= 4; i++)
  43.             {
  44.                 System.out.println((i+1)+"° número: "+x[i]);
  45.             }
  46.    
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement