diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e60f755..bcc1af3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: java-version: '17' distribution: 'adopt' - run: java src/algoritmos/Quicksort.java + - run: java src/algoritmos/Bubblesort.java - run: java src/algoritmos/RadixSort.java - - run: java src/algoritmos/BubbleSort.java - run: java src/algoritmos/MergeSort.java - - run: java src/algoritmos/SelectionSort.java \ No newline at end of file + - run: java src/algoritmos/SelectionSort.java diff --git a/src/algoritmos/BubbleSort.java b/src/algoritmos/BubbleSort.java deleted file mode 100644 index 7dd1703..0000000 --- a/src/algoritmos/BubbleSort.java +++ /dev/null @@ -1,40 +0,0 @@ -package algoritmos; - -import java.io.IOException; - -public class BubbleSort { - - public static void main(String[] args) throws IOException { - int quantidade = 10000; - int[] vetor = new int[quantidade]; - - System.out.println("Vetor desordenado: "); - for (int i = 0; i < vetor.length; i++) { - vetor[i] = (int) (Math.random() * quantidade); - System.out.print(i + " "); - } - - bubbleSort(vetor); - - System.out.println("\nVetor ordenado: "); - for (int i : vetor) { - System.out.print(i + " "); - } - } - - private static void bubbleSort(int vetor[]) { - boolean troca = true; - int aux; - while (troca) { - troca = false; - for (int i = 0; i < vetor.length - 1; i++) { - if (vetor[i] > vetor[i + 1]) { - aux = vetor[i]; - vetor[i] = vetor[i + 1]; - vetor[i + 1] = aux; - troca = true; - } - } - } - } -} \ No newline at end of file diff --git a/src/algoritmos/Bubblesort.java b/src/algoritmos/Bubblesort.java new file mode 100644 index 0000000..008bfa7 --- /dev/null +++ b/src/algoritmos/Bubblesort.java @@ -0,0 +1,41 @@ +package algoritmos; + +public class BubbleSortMelhorado { + public static void main(String args[]) { + int[] v = {0, 1, 2, 3}; + BubbleSortMelhorado bs = new BubbleSortMelhorado(); + bs.ordenar(v); + for(int num : v) { + System.out.print(num + " "); + } + } + + /** + * Método que ordena um vetor de inteiros utilizando o algoritmo + * de Bubble Sort. + * + * @param v - Vetor que será ordenado. + */ + public void ordenar(int[] v) { + /* for utilizado para controlar a quantidade de vezes que o vetor será + ordenado. */ + for(int i = 0; i < v.length - 1; i++) { + // Variavel utilizada para controlar se o vetor ja está ordenado. + boolean estaOrdenado = true; + // for utilizado para ordenar o vetor. + for(int j = 0; j < v.length - 1 - i; j++) { + /* Se o valor da posição atual do vetor for maior que o proximo valor, + então troca os valores de lugar no vetor. */ + if(v[j] > v[j + 1]) { + int aux = v[j]; + v[j] = v[j + 1]; + v[j + 1] = aux; + estaOrdenado = false; + } + } + // Se o vetor está ordenado então para as iterações sobre ele. + if(estaOrdenado) + break; + } + } +} \ No newline at end of file diff --git a/src/algoritmos/MergeSort.java b/src/algoritmos/MergeSort.java index a1ad3c3..26bb694 100644 --- a/src/algoritmos/MergeSort.java +++ b/src/algoritmos/MergeSort.java @@ -2,7 +2,7 @@ import java.io.IOException; -public class Mergesort { +public class MergeSort { public static void main(String[] args) throws IOException { int quantidade = 10000; @@ -45,7 +45,9 @@ private static void mergeSort(int[] a, int n) { private static void merge( int[] a, int[] l, int[] r, int left, int right) { - int i = 0, j = 0, k = 0; + int i = 0; + int j = 0; + int k = 0; while (i < left && j < right) { if (l[i] <= r[j]) { a[k++] = l[i++]; diff --git a/src/algoritmos/Quicksort.java b/src/algoritmos/Quicksort.java index 9f109aa..2699a48 100644 --- a/src/algoritmos/Quicksort.java +++ b/src/algoritmos/Quicksort.java @@ -30,7 +30,8 @@ private static void quickSort(int[] vetor, int inicio, int fim) { private static int posicaoPivor(int[] vetor, int inicio, int fim) { int pivo = vetor[inicio]; - int i = inicio + 1, f = fim; + int i = inicio + 1; + int f = fim; while (i <= f) { if (vetor[i] <= pivo) i++; diff --git a/src/algoritmos/SelectionSort.java b/src/algoritmos/SelectionSort.java index c7d5ef7..c4d1ba7 100644 --- a/src/algoritmos/SelectionSort.java +++ b/src/algoritmos/SelectionSort.java @@ -1,7 +1,5 @@ package algoritmos; -import java.io.IOException; - public class SelectionSort { public static void main(String args[]) { diff --git a/src/exemplos/Main.java b/src/exemplos/Main.java new file mode 100644 index 0000000..c2d1de7 --- /dev/null +++ b/src/exemplos/Main.java @@ -0,0 +1,47 @@ +package exemplos; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class Main { + + public static void main(String[] args) { + // TODO Auto-generated method stub + f(10); + readFile(""); + validarString("omnilink"); + } + + public static long f(int n) { + if (n < 2) { + return n; + } else { + return f(n - 1) + f(n - 2); + } + } + + public static String readFile(String fileName){ + BufferedReader br; + try { + br = new BufferedReader(new FileReader(fileName)); + final StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + sb.append(System.lineSeparator()); + line = br.readLine(); + } + br.close(); + return sb.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + public static boolean validarString(String entrada) { + return entrada != null && !entrada.equals("") && entrada.length() > 3; + } +}