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 ShellSort { public static void main(String[] args) { Integer[] lista = new Integer[]{2, 8, 4, 1, 3, 7, 5, 6}; shellSort(lista); Util.mostraVetor(lista); } public static void shellSort(Integer[] lista) { int qtdTrocas = 0; int h = 4; int n = lista.length; // Inicialização de h while(h < n) { h = h * 3 + 1; } h = h / 3; int c, j; while (h > 0) { // Laço para h-ordenar os vetores for (int i = h; i < n; i++) { c = lista[i]; j = i; while (j >= h && lista[j - h] > c) { lista[j] = lista[j - h]; j = j - h; } lista[j] = c; qtdTrocas++; } // Atualização do valor de h h = h / 2; } System.out.println(qtdTrocas + " trocas realizadas."); } }
Compartilhar