Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. package me.andrefma.testearquivo;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Ordenacao {
  6.     public static String[] bubbleSort(String[] A) {
  7.         for (int i = A.length; i >= 1; i--) {
  8.             for (int j = 1; j < i; j++) {
  9.                 if (A[j - 1].length() > A[j].length()) {
  10.                     String aux = A[j];
  11.                     A[j] = A[j - 1];
  12.                     A[j - 1] = aux;
  13.                 }
  14.             }
  15.         }
  16.         return A;
  17.     }
  18.     public static String[] selectionSort(String[] A) {
  19.         for (int i = 0; i < A.length; i++) {
  20.             int minIndx = i;
  21.             for (int j = i + 1; j < A.length; j++) {
  22.                 if(A[j].length() < A[minIndx].length()) {
  23.                     minIndx = j;
  24.                 }
  25.                 String aux = A[minIndx];
  26.                 A[minIndx] = A[i];
  27.                 A[i] = aux;
  28.             }
  29.         }
  30.         return A;
  31.     }
  32.     public static String[] insertionSort(String[] A) {
  33.         for (int i = 1; i < A.length; i++) {
  34.             String key = A[i];
  35.             int j = i - 1;
  36.             while(j >= 0 && A[j].length() > key.length()) {
  37.                 A[j + 1] = A[j];
  38.                 j = j - 1;
  39.             }
  40.             A[j + 1] = key;
  41.         }
  42.         return A;
  43.     }
  44.     public static String[] mergeSort(String[] A, int low, int high) {
  45.         if (low < high) {
  46.             int middle = (low + high) / 2;
  47.             mergeSort(A, low, middle);
  48.             mergeSort(A, middle + 1, high);
  49.             merge(A, low, middle, high);
  50.         }
  51.         return A;
  52.     }
  53.     private static void merge(String[] A, int low, int middle, int high) {
  54.         String[] aux = new String[A.length];
  55.         for (int i = low; i <= high; i++) {
  56.             aux[i] = A[i];
  57.         }
  58.         int auxL = low;
  59.         int auxR = middle + 1;
  60.         int k = low;
  61.         while (auxL <= middle && auxR <= high) {
  62.             if (aux[auxL].length() <= aux[auxR].length()) {
  63.                 A[k] = aux[auxL];
  64.                 auxL++;
  65.             }else {
  66.                 A[k] = aux[auxR];
  67.                 auxR++;
  68.             }
  69.             k++;
  70.         }
  71.         int restante = middle - auxL;
  72.         for (int i = 0; i <= restante; i++) {
  73.             A[k + i] = aux[auxL + i];
  74.         }
  75.     }
  76.     public static int partition(String[] A, int low, int high) {
  77.         int pivot = A[high].length();
  78.         int i = (low - 1);
  79.         for (int j = low; j < high; j++) {
  80.             if(A[j].length() <= pivot) {
  81.                 i++;
  82.                 String aux = A[i];
  83.                 A[i] = A[j];
  84.                 A[j] = aux;
  85.             }
  86.         }
  87.         String aux = A[i + 1];
  88.         A[i + 1] = A[high];
  89.         A[high] = aux;
  90.         return i + 1;
  91.     }
  92.     public static String[] quickSort(String[] A, int low, int high) {
  93.         if(low < high) {
  94.             int pi = partition(A, low, high);
  95.             quickSort(A, low, pi - 1);
  96.             quickSort(A, pi + 1, high);
  97.         }
  98.         return A;
  99.     }
  100.     public static int buscaBinaria(String[] A, String search) {
  101.         Arrays.sort(A);
  102.         /*Ordena o Vetor[] de forma alfabetica, pois o enunciado pede ordenacao por
  103.         tamanho da String e a buscaBinaria funciona com ordenacao alfabetica*/
  104.         int low = 0;
  105.         int high = A.length - 1;
  106.         int middle;
  107.         while (low <= high) {
  108.             middle = (low + high) / 2;
  109.  
  110.             if (A[middle].compareTo(search) < 0) {
  111.                 low = middle + 1;
  112.             } else if (A[middle].compareTo(search) > 0) {
  113.                 high = middle - 1;
  114.             } else {
  115.                 return middle;
  116.             }
  117.         }
  118.         return -1;
  119.     }
  120.     public static String buscaSequencial(String[] A, String search) {
  121.         for (int i = 0; i < A.length; i++) {
  122.             if(A[i].equalsIgnoreCase(search)) {
  123.                 return search + " está presente no Vetor[] na posição " + i;
  124.             }
  125.         }
  126.         return search + " não está presente no Vetor[]";
  127.     }
  128.     public static void imprimeVetor(String[] A) {
  129.         for(int i = 0; i < A.length; i++) {
  130.             System.out.println(A[i]);
  131.         }
  132.     }
  133.     private static long startMillis;
  134.     private static long startNano;
  135.     private static long finishMillis;
  136.     private static long finishNano;
  137.     public static void startTime() {
  138.         startMillis = System.currentTimeMillis();
  139.         startNano = System.nanoTime();
  140.     }
  141.     public static void finishTime() {
  142.         finishMillis = System.currentTimeMillis() - startMillis;
  143.         finishNano = System.nanoTime() - startNano;
  144.     }
  145.     public static void printTime() {
  146.         System.out.println(finishMillis+" milisegundos\n" + finishNano+" nanosegundos");
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement