Buscar

BubbleSort

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

package facema.classpesq.ordenacao;
import facema.Util;
public class BubbleSort {
	public static void main(String[] args) {
		System.out.println("BUBBLE SORT");
		
		Integer[] vetor = criarVetorGrande();
		
		long t0 = System.currentTimeMillis();
		
		bubbleSort(vetor);
		
		long tf = System.currentTimeMillis();
		
		System.out.println("Tempo: " + (tf-t0) + " milissegundos");
//		System.out.println("\nDepois: ");
//		Util.mostraVetor(vetor);
	}
	
	private static Integer[] criarVetorGrande() {
		Integer[] vetor = new Integer[100000];
		for (int i = 0; i < vetor.length; i++) {
			vetor[i] = 100000-i;
		}
		return vetor;
	}
	public static void bubbleSort(Integer[] lista) {
		Integer aux;
		int qtdTrocas = 0;
		
		for (int i = 0; i < lista.length - 1; i++) {
			for(int j = 0; j < lista.length - 1 - i; j++) {
				if (lista[j] > lista[j+1]) {
					aux = lista[j];
					lista[j] = lista[j+1];
					lista[j+1] = aux;
					qtdTrocas++;
				}
			}
		}
		
		System.out.println("Trocas realizadas: " + qtdTrocas);
	}
	
}

Teste o Premium para desbloquear

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

Outros materiais

Materiais relacionados

Perguntas relacionadas

Perguntas Recentes