Buscar

InsertionSort

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

package facema.classpesq.ordenacao;
import facema.Util;
public class InsertionSort {
	public static void main(String[] args) {
		Integer[] vetor = new Integer[]{4,3,6,5,1,7,2,8};
		
		insertionSort(vetor);
		
		Util.mostraVetor(vetor);
	}
	
	private static Integer[] criarVetorGrande() {
		Integer[] vetor = new Integer[200000];
		for (int i = 0; i < vetor.length; i++) {
			vetor[i] = 200000-i;
		}
		return vetor;
	}
	public static void insertionSort(Integer[] vetor) {
		int qtdTrocas = 0;
		
		for (int i = 1; i < vetor.length; i++) {
			for (int j = i; j >= 1 && vetor[j] > vetor[j-1]; j--) {
				Integer aux = vetor[j];
				vetor[j] = vetor[j-1];
				vetor[j-1] = aux;
				qtdTrocas++;
			}
		}
		
		System.out.println("Quantidade de trocas: " + qtdTrocas);
	}
	
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando

Outros materiais