Advertisement
LightProgrammer000

Bubble Sort

Feb 19th, 2020
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. // Pacote
  2. package Extra;
  3.  
  4. // Bibliotecas
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class EX_02
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         // Variaveis
  13.         ArrayList lista = new ArrayList(); // Lista
  14.         double vetor [] = new double[3]; // Vetor
  15.        
  16.         // Instanciacao
  17.         Scanner ent = new Scanner(System.in);
  18.        
  19.         // Estrutura de repeticao: Populando vetor
  20.         for (int i = 0; i < 3; i++)
  21.         {
  22.             System.out.printf("# Numero [%d]: ", i+1);
  23.             vetor[i] = ent.nextDouble();
  24.         }
  25.        
  26.         // Chamada de metodo: Atribuindo funcao
  27.         double[] vetor_lista = ordem_crescente(vetor);
  28.        
  29.         // Estrutura de repeticao: Populando lista
  30.         for (int i = 0; i < vetor_lista.length; i++)
  31.         {
  32.             // Adicionando valores na lista a partir do vetor
  33.             lista.add(vetor[i]);
  34.         }
  35.  
  36.         // Chamada de metodo: Imp_lista
  37.         imp_lista(lista);
  38.  
  39.     }
  40.    
  41.     // Funcao: Bubble Sort (ordem crescente)
  42.     static double[] ordem_crescente(double vetor[])
  43.     {
  44.         // Variavel
  45.         double aux;
  46.        
  47.         // Estrutura de repeticao: i
  48.         for (int i = 0; i < vetor.length; i++)
  49.         {
  50.             // Estrutura de repeticao: j
  51.             for (int j = 0; j < i; j++)
  52.             {
  53.                 // Estrutura de decisao
  54.                 if (vetor[i] < vetor[j])
  55.                 {
  56.                     aux = vetor[i];
  57.                     vetor[i] = vetor[j];
  58.                     vetor[j] = aux;
  59.                 }
  60.             }
  61.         }
  62.        
  63.        // Retorno do vetor
  64.        return vetor;
  65.     }
  66.    
  67.     static void imp_lista(ArrayList lista)
  68.     {
  69.         System.out.println("\n------ RELATORIO ------");
  70.        
  71.         lista.stream().forEach((w) ->    
  72.                 System.out.println("# " + w));
  73.        
  74.         System.out.println("-----------------------");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement