Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
package facema.classpesq.ordenacao; import facema.Util; public class SelectionSort { public static void main(String[] args) { System.out.println("SELECTION SORT"); Integer[] vetor = new Integer[]{5,7,3,1,4,6,2,8}; long t0 = System.currentTimeMillis(); selectionSort(vetor); long tf = System.currentTimeMillis(); System.out.println("Tempo: " + (tf-t0) + " milissegundos"); Util.mostraVetor(vetor); } private static Integer[] criarVetorGrande() { Integer[] vetor = new Integer[10000]; for (int i = 0; i < vetor.length; i++) { vetor[i] = 10000-i; } return vetor; } public static void selectionSort(Integer[] vetor) { int qtdTrocas = 0; // Percorrer o vetor for (int iteracao = 0; iteracao < vetor.length - 1; iteracao++) { int menor = iteracao; // Buscar pelo menor elemento naquela iteração for (int i = menor + 1; i < vetor.length; i++) if (vetor[i] < vetor[menor]) menor = i; if (menor != iteracao) { // Realizar a troca int aux = vetor[iteracao]; vetor[iteracao] = vetor[menor]; vetor[menor] = aux; qtdTrocas++; } } System.out.println("Trocas realizadas: " + qtdTrocas); } }
Compartilhar