Você está na página 1de 7

Projeto e Análise de Algoritmos

Códigos Implementados
Metodo BOLHA – Bubble Sort
import java.io.IOException;

public class Bolha {

public static int cont = 0;

public static void main(String[] args) throws IOException {


int n = 100;
int[] vetor = new int[n];
for (int i = 0; i < vetor.length; i++) {
vetor[i] = (int) (Math.random() * n); //Aleatorio
//vetor[i]=quantidade-i; //decrescente
//vetor[i]=i+1; //crescente
}

for (int i = 0; i < n; i++) {


System.out.print(" " + vetor[i]);
}

Bolha bolha = new Bolha();


bolha.Bolha(vetor);

System.out.print(" \n Vetor ordenado:\n");

for (int i = 0; i < n; i++) {


System.out.print(" " + vetor[i]);
}

System.out.print(" \n Contador: " + cont);

public int Bolha(int vetor[]) {


boolean altera = true;
int aux;
while (altera) {
altera = false;
for (int i = 0; i < vetor.length - 1; i++) {
cont++;
if (vetor[i] > vetor[i + 1]) {
aux = vetor[i];
vetor[i] = vetor[i + 1];
vetor[i + 1] = aux;
altera = true;
}
}
}
return cont;
}

}
Projeto e Análise de Algoritmos
Códigos Implementados
Metodo HEAP SORT

public class HeapSort {


public static int cont = 0;
private static int[] a;
private static int n;
private static int left;
private static int right;
private static int largest;

public static void buildheap(int[] a) {

n = a.length - 1;
for (int i = n / 2; i >= 0; i--) {

maxheap(a, i);

}
}

public static void maxheap(int[] a, int i) {


cont++;
left = 2 * i;
right = 2 * i + 1;
if (left <= n && a[left] > a[i]) {

largest = left;

} else {

largest = i;
}

if (right <= n && a[right] > a[largest]) {

largest = right;
}
if (largest != i) {

exchange(i, largest);
maxheap(a, largest);

}
}

public static void exchange(int i, int j) {


int t = a[i];
a[i] = a[j];
a[j] = t;
}

public static void sort(int[] a0) {


a = a0;
buildheap(a);
Projeto e Análise de Algoritmos
Códigos Implementados
for (int i = n; i > 0; i--) {

exchange(0, i);
n = n - 1;
maxheap(a, 0);
}
}

public static void main(String[] args) {


int tamanho;
tamanho = 500;
int[] vetor = new int[tamanho];

for (int i = 0; i < vetor.length; i++) {


//vetor[i] = i;//crescente
vetor[i]=tamanho-i;//decrescente
//vetor[i] = (int) (Math.random() * tamanho);//
aleatorio
System.out.print(vetor[i] + " - ");
}
for (int i = 0; i < vetor.length; i++) {
System.out.print(vetor[i] + " ");

}
sort(vetor);
System.out.println("\n HeapSort:");
for (int i = 0; i < vetor.length; i++) {
System.out.print(vetor[i] + " ");

}
System.out.println("\n Contador= " + cont);
}
}
Projeto e Análise de Algoritmos
Códigos Implementados
Metodo INSERÇÃO – Insertion Sort
public class Insercao {

public static int contador = 0;

public static void main(String[] args) {


int tamanho = 100;
int[] vetor = new int[tamanho];

for (int i = 0; i < vetor.length; i++) {


//vetor[i] = i;//crescente
//vetor[i]=tamanho-i;//decrescente
vetor[i]=(int) (Math.random()*tamanho);//aleatorio
System.out.print(vetor[i] + " - ");
}

System.out.println("\n Números ordenados:");


for (int num : insertionSort(vetor)) {
System.out.print(num + " - ");
}

System.out.println("\n Contador= " + contador);

public static int[] insertionSort(int[] numeros) {

int i, j, eleito;

for (i = 1; i < numeros.length; i++) {


contador++;
eleito = numeros[i];

j = i;

while ((j > 0) && (numeros[j - 1] > eleito)) {


contador++;
numeros[j] = numeros[j - 1];
j = j - 1;
}
numeros[j] = eleito;
}

return numeros;
}
}
Projeto e Análise de Algoritmos
Códigos Implementados
Metodo QUICK SORT

public class Quicksort {

private int array[];


private int length;
public static int contador = 0;

public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {


return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}

private void quickSort(int lowerIndex, int higherIndex) {

int i = lowerIndex;
int j = higherIndex;

int pivot = array[lowerIndex + (higherIndex - lowerIndex) /


2];

while (i <= j) {
contador++;
while (array[i] < pivot) {
contador++;
i++;
}
while (array[j] > pivot) {
j--;
contador++;
}
if (i <= j) {
exchangeNumbers(i, j);

i++;
j--;
}
}

if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}

private void exchangeNumbers(int i, int j) {


int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
Projeto e Análise de Algoritmos
Códigos Implementados

public static void main(String a[]) {

Quicksort sorter = new Quicksort();

int tamanho = 100;


int[] input = new int[tamanho];
for (int i = 0; i < input.length; i++) {
//input[i] = tamanho-i; //Decrescente
//input[i] = (int) (Math.random()*tamanho);//set vetor
para aleatorio
input[i]=i+1; //crescente
System.out.print(input[i] + " - ");
}

System.out.print(" \n Vetor ordenado:\n");

sorter.sort(input);
for (int i : input) {
System.out.print(i + " - ");
System.out.print(" ");
}

System.out.println("\n Contador: " + contador);


}
}
Projeto e Análise de Algoritmos
Códigos Implementados
Metodo Seleção – Selection Sort
public class Selecao {

static int tamanho = 400;

public static int contador =0;

public static void selecao(int[] vetor) {


int aux, menor;
for (int i = 0; i < vetor.length - 1; i++) {
menor = i;
for (int j = i + 1; j < vetor.length; j++) {
contador++;
if (vetor[j] < vetor[menor]) {
menor = j;
}
}

aux = vetor[i];
vetor[i] = vetor[menor];
vetor[menor] = aux;
}

public static void main(String[] args) {


int meuVetor[];

meuVetor = new int[tamanho];

for (int i = 0; i < tamanho; i++) {


meuVetor[i] = (int) (Math.random()*tamanho);//set vetor para
aleatorio
//meuVetor[i]= tamanho-i; //decrescente
//meuVetor[i]=i+1;//crescente

}
System.out.println("Vetor inicial: \n");
for (int i = 0; i < tamanho; i++) {
System.out.print(" " + meuVetor[i]);
}

selecao(meuVetor);
System.out.println("\n \n Vetor ordenado: \n");
for (int i = 0; i < tamanho; i++) {
System.out.print(" " + meuVetor[i]);
}

System.out.println("\n\n contador = " + contador + "\n");

}
}

Você também pode gostar