Advertisement
aleffelixf

Execution time insert sort

Sep 21st, 2021
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void bubble(int[] array) {
  4.         for (int i = 1; i <= array.length - 1; i++) {
  5.             for (int j=0; j < array.length-1; j++) {
  6.                 if (array[j] > array[j + 1]) {
  7.                     int aux = array[j];
  8.                     array[j] = array[j + 1];
  9.                     array[j + 1] = aux;
  10.                 }
  11.             }
  12.         }
  13.     }
  14.  
  15.     public static void selection(int[] array) {
  16.         for (int i=0; i < array.length-1; i++) {
  17.             int lowestIndex = i;
  18.             for(int j=i+1; j < array.length; j++) {
  19.                 if(array[j] < array[lowestIndex])
  20.                     lowestIndex = j;
  21.             }
  22.             int aux = array[i];
  23.             array[i] = array[lowestIndex];
  24.             array[lowestIndex] = aux;
  25.         }
  26.     }
  27.  
  28.     public static void insercao(int vet[]) {
  29.         for(int i = 1; i < vet.length; i++){
  30.             int aux = vet[i];
  31.             int j = i - 1;        
  32.             while (j >= 0 && vet[j] < aux) {
  33.                vet[j + 1] = vet[j];
  34.                j--;
  35.             }
  36.             vet[j + 1] = aux;
  37.          }
  38.     }
  39.    
  40.     public static void main(String[] args) {
  41.         String response1 = "\nMetodo da bolha\n";
  42.         String response2 = "\nMetodo da Seleção\n";
  43.         String response3 = "\nMetodo da Inserção\n";
  44.  
  45.         for (int tam = 5000; tam <= 40000; tam = tam + 5000) {
  46.             int[] array1 = new int[tam];
  47.             int[] array2 = new int[tam];
  48.             int[] array3 = new int[tam];
  49.  
  50.             for (int i = 0; i < tam; i++)
  51.                 array1[i] = array2[i] = array3[i] = (int) (Math.random() * 1000);
  52.  
  53.             long initialTime = System.currentTimeMillis();
  54.             bubble(array1);
  55.             long finalTime = System.currentTimeMillis();
  56.             response1 += "Tamanho = " + tam + "  => Tempo = " + (finalTime - initialTime) + " ms\n";
  57.  
  58.             initialTime = System.currentTimeMillis();
  59.             selection(array2);
  60.             finalTime = System.currentTimeMillis();
  61.             response2 += "Tamanho = " + tam + "  => Tempo = " + (finalTime - initialTime) + " ms\n";
  62.  
  63.             initialTime = System.currentTimeMillis();
  64.             insercao(array3);
  65.             finalTime = System.currentTimeMillis();
  66.             response3 += "Tamanho = " + tam + "  => Tempo = " + (finalTime - initialTime) + " ms\n";
  67.         }
  68.  
  69.         System.out.println(response1);
  70.         System.out.println(response2);
  71.         System.out.println(response3);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement