Merge pull request #6 from icei-pucminas/ordenacao-header-c

Atualização nos algoritmos de ordenação
This commit is contained in:
Max do Val Machado 2020-08-23 20:53:54 -03:00 committed by GitHub
commit ff7968080e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
50 changed files with 478 additions and 481 deletions

View File

@ -0,0 +1,14 @@
#include "geracao.h"
#include "bolha.h"
//=============================================================================
void bolha(int *array, int n){
int i, j;
for (i = (n - 1); i > 0; i--) {
for (j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
swap(&array[j], &array[j + 1]);
}
}
}
}
//=============================================================================

View File

@ -1,17 +1,6 @@
#ifndef BOLHA_H #ifndef BOLHA_H
#define BOLHA_H #define BOLHA_H
//============================================================================= //=============================================================================
#include "geracao.h" void bolha(int *array, int n);
//============================================================================= //=============================================================================
void bolha(int *array, int n){ #endif
int i, j;
for (i = (n - 1); i > 0; i--) {
for (j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
swap(&array[j], &array[j + 1]);
}
}
}
}
//=============================================================================
#endif

Binary file not shown.

View File

@ -1,64 +0,0 @@
#include "bolha.h"
//=============================================================================
int main(int argc, char **argv) {
int n, *array;
clock_t comeco, fim;
double total;
srand(time(NULL));
if(argc < 2){
printf("Execute: %s n\n", argv[0]);
printf("n - tamanho do vetor (int)\n");
return 0;
}
n = atoi(argv[1]);
if(n <= 0){
printf("Erro: n deve ser > 0\n");
return 0;
}
array = (int*)malloc(n*sizeof(int));
printf("Teste Bolha: Ordem Crescente\n");
crescente(array, n);
mostrar(array, n);
comeco = clock();
bolha(array, n);
fim = clock();
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
mostrar(array, n);
printf("Tempo para ordenar: %f ms (%s).\n",
total,
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
printf("\nTeste Bolha: Ordem Decrescente\n");
decrescente(array, n);
mostrar(array, n);
comeco = clock();
bolha(array, n);
fim = clock();
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
mostrar(array, n);
printf("Tempo para ordenar: %f ms (%s).\n",
total,
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
printf("\nTeste Bolha: Ordem Aleatoria\n");
aleatorio(array, n);
mostrar(array, n);
comeco = clock();
bolha(array, n);
fim = clock();
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
mostrar(array, n);
printf("Tempo para ordenar: %f ms (%s).\n",
total,
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
free(array);
return 0;
}

View File

@ -0,0 +1,36 @@
#include "geracao.h"
#include "countingsort.h"
//=============================================================================
int getMaior(int *array, int n) {
int maior = array[0];
for (int i = 0; i < n; i++) {
if(maior < array[i]){
maior = array[i];
}
}
return maior;
}
//=============================================================================
void countingsort(int *array, int n) {
//Array para contar o numero de ocorrencias de cada elemento
int tamCount = getMaior(array, n) + 1;
int count[tamCount];
int ordenado[n];
//Inicializar cada posicao do array de contagem
for (int i = 0; i < tamCount; count[i] = 0, i++);
//Agora, o count[i] contem o numero de elemento iguais a i
for (int i = 0; i < n; count[array[i]]++, i++);
//Agora, o count[i] contem o numero de elemento menores ou iguais a i
for(int i = 1; i < tamCount; count[i] += count[i-1], i++);
//Ordenando
for(int i = n-1; i >= 0; ordenado[count[array[i]]-1] = array[i], count[array[i]]--, i--);
//Copiando para o array original
for(int i = 0; i < n; array[i] = ordenado[i], i++);
}
//=============================================================================

View File

@ -1,39 +1,8 @@
#ifndef COUNTINGSORT_H #ifndef COUNTINGSORT_H
#define COUNTINGSORT_H #define COUNTINGSORT_H
//============================================================================= //=============================================================================
#include "geracao.h" int getMaior(int *array, int n);
//============================================================================= //=============================================================================
int getMaior(int *array, int n) { void countingsort(int *array, int n);
int maior = array[0];
for (int i = 0; i < n; i++) {
if(maior < array[i]){
maior = array[i];
}
}
return maior;
}
//============================================================================= //=============================================================================
void countingsort(int *array, int n) { #endif
//Array para contar o numero de ocorrencias de cada elemento
int tamCount = getMaior(array, n) + 1;
int count[tamCount];
int ordenado[n];
//Inicializar cada posicao do array de contagem
for (int i = 0; i < tamCount; count[i] = 0, i++);
//Agora, o count[i] contem o numero de elemento iguais a i
for (int i = 0; i < n; count[array[i]]++, i++);
//Agora, o count[i] contem o numero de elemento menores ou iguais a i
for(int i = 1; i < tamCount; count[i] += count[i-1], i++);
//Ordenando
for(int i = n-1; i >= 0; ordenado[count[array[i]]-1] = array[i], count[array[i]]--, i--);
//Copiando para o array original
for(int i = 0; i < n; array[i] = ordenado[i], i++);
}
//=============================================================================
#endif

View File

@ -0,0 +1,70 @@
#include "geracao.h"
//=============================================================================
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
//=============================================================================
// PROCEDIMENTO PARA TROCAR DOIS ELEMENTOS DO VETOR
void swap(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
//=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM CRESCENTE
void crescente(int *array, int n) {
for (int i = 0; i < n; i++) {
array[i] = i;
}
}
//=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM DECRESCENTE
void decrescente(int *array, int n) {
for (int i = 0; i < n; i++) {
array[i] = n - 1 - i;
}
}
//=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM ALEATORIA
void aleatorio(int *array, int n) {
crescente(array, n);
srand(time(NULL));
for (int i = 0; i < n; i++) {
swap(&array[i], &array[rand() % n]);
}
}
//=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS DA ENTRADA PADRAO
int entradaPadrao(int *array) {
int n;
scanf("%i", &n);
array = (int*) malloc(n*sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%i", &array[i]);
}
return n;
}
//=============================================================================
// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO ARRANJO
void mostrar(int *array, int n) {
printf("[ ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("] \n");
}
//=============================================================================
// PROCEDIMENTO PARA VERIFICAR SE O ARRANJO ESTA ORDENADO
bool isOrdenado(int *array, int n){
bool resp = true;
for(int i = 1; i < n; i++){
if(array[i-1] > array[i]){
i = n;
resp = false;
}
}
return resp;
}
//=============================================================================

View File

@ -1,65 +1,18 @@
#ifndef GERACAO_H #ifndef GERACAO_H
#define GERACAO_H #define GERACAO_H
//============================================================================= //=============================================================================
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA TROCAR DOIS ELEMENTOS DO VETOR void swap(int *i, int *j);
void swap(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM CRESCENTE void crescente(int *array, int n);
void crescente(int *array, int n) {
int i;
for (i = 0; i < n; i++) {
array[i] = i;
}
}
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM DECRESCENTE void decrescente(int *array, int n);
void decrescente(int *array, int n) {
int i;
for (i = 0; i < n; i++) {
array[i] = n - 1 - i;
}
}
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM ALEATORIA void aleatorio(int *array, int n);
void aleatorio(int *array, int n) {
int i, pos;
crescente(array, n);
srand(time(NULL));
for (i = 0; i < n; i++) {
pos = rand() % n;
swap(&array[i], &array[pos]);
}
}
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO ARRANJO void mostrar(int *array, int n);
void mostrar(int *array, int n) {
int i;
printf("[ ");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("] \n");
}
//============================================================================= //=============================================================================
// PROCEDIMENTO PARA VERIFICAR SE O ARRANJO ESTA ORDENADO bool isOrdenado(int *array, int n);
bool isOrdenado(int *array, int n){
int i;
for(int i = 1; i < n; i++){
if(array[i-1] > array[i]){
return false;
}
}
return true;
}
//============================================================================= //=============================================================================
#endif #endif

View File

@ -0,0 +1,57 @@
#include "heapsort.h"
#include "geracao.h"
//=============================================================================
void constroi(int *array, int tamHeap){
for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){
swap(array + i, array + i/2);
}
}
//=============================================================================
int getMaiorFilho(int *array, int i, int tamHeap){
int filho;
if (2*i == tamHeap || array[2*i] > array[2*i+1]){
filho = 2*i;
} else {
filho = 2*i + 1;
}
return filho;
}
//=============================================================================
void reconstroi(int *array, int tamHeap){
int i = 1;
while(i <= (tamHeap/2)){
int filho = getMaiorFilho(array, i, tamHeap);
if(array[i] < array[filho]){
swap(array + i, array + filho);
i = filho;
}else{
i = tamHeap;
}
}
}
//=============================================================================
void heapsort(int *array, int n) {
//Alterar o vetor ignorando a posicao zero
int arrayTmp[n+1];
for(int i = 0; i < n; i++){
arrayTmp[i+1] = array[i];
}
//Contrucao do heap
for(int tamHeap = 2; tamHeap <= n; tamHeap++){
constroi(arrayTmp, tamHeap);
}
//Ordenacao propriamente dita
int tamHeap = n;
while(tamHeap > 1){
swap(arrayTmp + 1, arrayTmp + tamHeap--);
reconstroi(arrayTmp, tamHeap);
}
//Alterar o vetor para voltar a posicao zero
for(int i = 0; i < n; i++){
array[i] = arrayTmp[i+1];
}
}
//=============================================================================

View File

@ -1,60 +1,12 @@
#ifndef HEAPSORT_H #ifndef HEAPSORT_H
#define HEAPSORT_H #define HEAPSORT_H
//============================================================================= //=============================================================================
#include "geracao.h" void constroi(int *array, int tamHeap);
//============================================================================= //=============================================================================
void constroi(int *array, int tamHeap){ int getMaiorFilho(int *array, int i, int tamHeap);
for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){
swap(array + i, array + i/2);
}
}
//============================================================================= //=============================================================================
int getMaiorFilho(int *array, int i, int tamHeap){ void reconstroi(int *array, int tamHeap);
int filho;
if (2*i == tamHeap || array[2*i] > array[2*i+1]){
filho = 2*i;
} else {
filho = 2*i + 1;
}
return filho;
}
//============================================================================= //=============================================================================
void reconstroi(int *array, int tamHeap){ void heapsort(int *array, int n);
int i = 1;
while(i <= (tamHeap/2)){
int filho = getMaiorFilho(array, i, tamHeap);
if(array[i] < array[filho]){
swap(array + i, array + filho);
i = filho;
}else{
i = tamHeap;
}
}
}
//============================================================================= //=============================================================================
void heapsort(int *array, int n) { #endif
//Alterar o vetor ignorando a posicao zero
int arrayTmp[n+1];
for(int i = 0; i < n; i++){
arrayTmp[i+1] = array[i];
}
//Contrucao do heap
for(int tamHeap = 2; tamHeap <= n; tamHeap++){
constroi(arrayTmp, tamHeap);
}
//Ordenacao propriamente dita
int tamHeap = n;
while(tamHeap > 1){
swap(arrayTmp + 1, arrayTmp + tamHeap--);
reconstroi(arrayTmp, tamHeap);
}
//Alterar o vetor para voltar a posicao zero
for(int i = 0; i < n; i++){
array[i] = arrayTmp[i+1];
}
}
//=============================================================================
#endif

View File

@ -0,0 +1,15 @@
#include "insercao.h"
//=============================================================================
void insercao(int *array, int n){
for (int i = 1; i < n; i++) {
int tmp = array[i];
int j = i - 1;
while ((j >= 0) && (array[j] > tmp)) {
array[j + 1] = array[j];
j--;
}
array[j+1] = tmp;
}
}
//=============================================================================

View File

@ -3,18 +3,6 @@
//============================================================================= //=============================================================================
#include "geracao.h" #include "geracao.h"
//============================================================================= //=============================================================================
void insercao(int *array, int n){ void insercao(int *array, int n);
int i, j, tmp;
for (int i = 1; i < n; i++) {
tmp = array[i];
int j = i - 1;
while ((j >= 0) && (array[j] > tmp)) {
array[j + 1] = array[j];
j--;
}
array[j+1] = tmp;
}
}
//============================================================================= //=============================================================================
#endif #endif

View File

@ -0,0 +1,40 @@
all: exec
exec: principal.o geracao.o bolha.o countingsort.o heapsort.o insercao.o mergesort.o quicksort.o selecao.o shellsort.o
gcc -o exec principal.o geracao.o bolha.o countingsort.o heapsort.o insercao.o mergesort.o quicksort.o selecao.o shellsort.o
principal.o: principal.c
gcc -o principal.o principal.c -c -W -Wall -pedantic
bolha.o: bolha.c
gcc -o bolha.o bolha.c -c -W -Wall -pedantic
countingsort.o: countingsort.c
gcc -o countingsort.o countingsort.c -c -W -Wall -pedantic
geracao.o: geracao.c
gcc -o geracao.o geracao.c -c -W -Wall -pedantic
heapsort.o: heapsort.c
gcc -o heapsort.o heapsort.c -c -W -Wall -pedantic
insercao.o: insercao.c
gcc -o insercao.o insercao.c -c -W -Wall -pedantic
mergesort.o: mergesort.c
gcc -o mergesort.o mergesort.c -c -W -Wall -pedantic
quicksort.o: quicksort.c
gcc -o quicksort.o quicksort.c -c -W -Wall -pedantic
selecao.o: selecao.c
gcc -o selecao.o selecao.c -c -W -Wall -pedantic
shellsort.o: shellsort.c
gcc -o shellsort.o shellsort.c -c -W -Wall -pedantic
clean:
rm -rf *.o *~ exec
limpa:
rm -rf *.o

View File

@ -0,0 +1,6 @@
#include "mergesort.h"
//=============================================================================
void mergesort(int *array, int n) {
array[0] = n;
}
//=============================================================================

View File

@ -3,4 +3,6 @@
//============================================================================= //=============================================================================
#include "geracao.h" #include "geracao.h"
//============================================================================= //=============================================================================
#endif void mergesort(int *array, int n);
//=============================================================================
#endif

View File

@ -6,6 +6,11 @@
#include "quicksort.h" #include "quicksort.h"
#include "selecao.h" #include "selecao.h"
#include "shellsort.h" #include "shellsort.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
//============================================================================= //=============================================================================
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -0,0 +1,22 @@
#include "quicksort.h"
//=============================================================================
void quicksortRec(int *array, int esq, int dir) {
int i = esq, j = dir;
int pivo = array[(dir+esq)/2];
while (i <= j) {
while (array[i] < pivo) i++;
while (array[j] > pivo) j--;
if (i <= j) {
swap(array + i, array + j);
i++;
j--;
}
}
if (esq < j) quicksortRec(array, esq, j);
if (i < dir) quicksortRec(array, i, dir);
}
//=============================================================================
void quicksort(int *array, int n) {
quicksortRec(array, 0, n-1);
}
//=============================================================================

View File

@ -3,24 +3,8 @@
//============================================================================= //=============================================================================
#include "geracao.h" #include "geracao.h"
//============================================================================= //=============================================================================
void quicksortRec(int *array, int esq, int dir) { void quicksortRec(int *array, int esq, int dir);
int i = esq, j = dir;
int pivo = array[(dir+esq)/2];
while (i <= j) {
while (array[i] < pivo) i++;
while (array[j] > pivo) j--;
if (i <= j) {
swap(array + i, array + j);
i++;
j--;
}
}
if (esq < j) quicksortRec(array, esq, j);
if (i < dir) quicksortRec(array, i, dir);
}
//============================================================================= //=============================================================================
void quicksort(int *array, int n) { void quicksort(int *array, int n);
quicksortRec(array, 0, n-1);
}
//============================================================================= //=============================================================================
#endif #endif

View File

@ -0,0 +1,14 @@
#include "selecao.h"
//=============================================================================
void selecao(int *array, int n){
for (int i = 0; i < (n - 1); i++) {
int menor = i;
for (int j = (i + 1); j < n; j++){
if (array[menor] > array[j]){
menor = j;
}
}
swap(&array[menor], &array[i]);
}
}
//=============================================================================

View File

@ -3,16 +3,6 @@
//============================================================================= //=============================================================================
#include "geracao.h" #include "geracao.h"
//============================================================================= //=============================================================================
void selecao(int *array, int n){ void selecao(int *array, int n);
for (int i = 0; i < (n - 1); i++) {
int menor = i;
for (int j = (i + 1); j < n; j++){
if (array[menor] > array[j]){
menor = j;
}
}
swap(&array[menor], &array[i]);
}
}
//============================================================================= //=============================================================================
#endif #endif

View File

@ -0,0 +1,27 @@
#include "shellsort.h"
//=============================================================================
void insercaoPorCor(int *array, int n, int cor, int h){
for (int i = (h + cor); i < n; i+=h) {
int tmp = array[i];
int j = i - h;
while ((j >= 0) && (array[j] > tmp)) {
array[j + h] = array[j];
j-=h;
}
array[j + h] = tmp;
}
}
//=============================================================================
void shellsort(int *array, int n) {
int h = 1;
do { h = (h * 3) + 1; } while (h < n);
do {
h /= 3;
for(int cor = 0; cor < h; cor++){
insercaoPorCor(array, n, cor, h);
}
} while (h != 1);
}
//=============================================================================

View File

@ -3,29 +3,8 @@
//============================================================================= //=============================================================================
#include "geracao.h" #include "geracao.h"
//============================================================================= //=============================================================================
void insercaoPorCor(int *array, int n, int cor, int h){ void insercaoPorCor(int *array, int n, int cor, int h);
for (int i = (h + cor); i < n; i+=h) {
int tmp = array[i];
int j = i - h;
while ((j >= 0) && (array[j] > tmp)) {
array[j + h] = array[j];
j-=h;
}
array[j + h] = tmp;
}
}
//============================================================================= //=============================================================================
void shellsort(int *array, int n) { void shellsort(int *array, int n);
int h = 1;
do { h = (h * 3) + 1; } while (h < n);
do {
h /= 3;
for(int cor = 0; cor < h; cor++){
insercaoPorCor(array, n, cor, h);
}
} while (h != 1);
}
//============================================================================= //=============================================================================
#endif #endif

View File

@ -1,7 +1,7 @@
/** /**
* Metodo de ordenacao da bolha * Metodo de ordenacao da bolha
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
class Bolha extends Geracao { class Bolha extends Geracao {
@ -25,7 +25,8 @@ class Bolha extends Geracao {
/** /**
* Algoritmo de ordenacao Bolha. * Algoritmo de ordenacao Bolha.
*/ */
public static void bolha() { @Override
public void sort() {
for (int i = (n - 1); i > 0; i--) { for (int i = (n - 1); i > 0; i--) {
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) { if (array[j] > array[j + 1]) {
@ -34,18 +35,4 @@ class Bolha extends Geracao {
} }
} }
} }
public static void main(String[] args) {
Bolha bolha = new Bolha(100);
bolha.aleatorio();
bolha.mostrar();
long comeco = now();
bolha.bolha();
long fim = now();
bolha.mostrar();
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s.");
}
} }

View File

@ -1,7 +1,7 @@
/** /**
* Metodo de ordenacao por contagem * Metodo de ordenacao por contagem
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
class Countingsort extends Geracao { class Countingsort extends Geracao {
@ -23,26 +23,11 @@ class Countingsort extends Geracao {
} }
/**
* Retorna o maior elemento do array.
* @return maior elemento
*/
public static int getMaior() {
int maior = array[0];
for (int i = 0; i < n; i++) {
if(maior < array[i]){
maior = array[i];
}
}
return maior;
}
/** /**
* Algoritmo de ordenacao Countingsort. * Algoritmo de ordenacao Countingsort.
*/ */
public static void countingsort() { @Override
public void sort() {
//Array para contar o numero de ocorrencias de cada elemento //Array para contar o numero de ocorrencias de cada elemento
int[] count = new int[getMaior() + 1]; int[] count = new int[getMaior() + 1];
int[] ordenado = new int[n]; int[] ordenado = new int[n];
@ -64,16 +49,18 @@ class Countingsort extends Geracao {
} }
public static void main(String[] args) { /**
Countingsort countingsort = new Countingsort(100); * Retorna o maior elemento do array.
countingsort.aleatorio(); * @return maior elemento
countingsort.mostrar(); */
public int getMaior() {
long comeco = now(); int maior = array[0];
countingsort.countingsort();
long fim = now(); for (int i = 0; i < n; i++) {
if(maior < array[i]){
countingsort.mostrar(); maior = array[i];
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s."); }
}
return maior;
} }
} }

View File

@ -1,17 +0,0 @@
class ExercicioDuvidaAND {
public static boolean m1(){
System.out.println("m1");
return false;
}
public static boolean m2(){
System.out.println("m2");
return true;
}
public static void main (String[] args) {
System.out.println("inicio");
boolean and = m1() && m2();
System.out.println("============");
boolean or = m2() || m1();
System.out.println("fim: and(" + and + ") or (" + or + ")");
}
}

View File

@ -1,14 +1,14 @@
/** /**
* Geracao de elementos de um array * Geracao de elementos de um array
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
import java.util.*; import java.util.*;
class Geracao { class Geracao {
protected static int[] array; protected int[] array;
protected static int n; protected int n;
/** /**
@ -33,30 +33,31 @@ class Geracao {
/** /**
* Produz um array ordenado de modo crescente. * Produz um array ordenado de modo crescente.
*/ */
public static void crescente() { public void crescente() {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
array[i] = i; array[i] = i;
} }
} }
/** /**
* Produz um array ordenado de modo decrescente. * Produz um array ordenado de modo decrescente.
*/ */
public static void decrescente() { public void decrescente() {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
array[i] = n - 1 - i; array[i] = n - 1 - i;
} }
} }
/** /**
* Produz um array de numeros aleatorios. * Produz um array com numeros aleatorios.
*/ */
public static void aleatorio() { public void aleatorio() {
Random gerador = new Random(); Random rand = new Random();
crescente();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
array[i] = Math.abs(gerador.nextInt()) % 1000; // 0 a 999 swap(i, Math.abs(rand.nextInt()) % n);
} }
} }
@ -64,7 +65,7 @@ class Geracao {
/** /**
* Efetua a leitura dos elementos via entrada padrao. * Efetua a leitura dos elementos via entrada padrao.
*/ */
public static void entradaPadrao() { public void entradaPadrao() {
n = MyIO.readInt(); n = MyIO.readInt();
array = new int[n]; array = new int[n];
@ -73,7 +74,10 @@ class Geracao {
} }
} }
public static void entrada(int[] vet){ /**
* Recebe um Efetua a leitura dos elementos via entrada padrao.
*/
public void entrada(int[] vet){
n = vet.length; n = vet.length;
array = new int[n]; array = new int[n];
for(int i = 0; i < n; i++){ for(int i = 0; i < n; i++){
@ -83,31 +87,31 @@ class Geracao {
/** /**
* Mostra os array de um array. * Mostra os k primeiros elementos do array.
* @param int k indica a quantidade de elementos do array a serem mostrados. * @param int k indica a quantidade de elementos do array a serem mostrados.
*/ */
public static void mostrar(int k) { public void mostrar(int k) {
System.out.print("[ "); System.out.print("[");
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
System.out.print(array[i] + " "); System.out.print(" ("+i+")" + array[i]);
} }
System.out.println("] "); System.out.println("]");
} }
/** /**
* Mostra os array de um array. * Mostra os elementos do array.
*/ */
public static void mostrar() { public void mostrar() {
System.out.print("[ "); System.out.print("[");
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
System.out.print("("+i+")" + array[i] + " "); System.out.print(" ("+i+")" + array[i]);
} }
System.out.println("] "); System.out.println("]");
} }
@ -116,7 +120,7 @@ class Geracao {
* @param i int primeira posicao * @param i int primeira posicao
* @param j int segunda posicao * @param j int segunda posicao
*/ */
public static void swap(int i, int j) { public void swap(int i, int j) {
int temp = array[i]; int temp = array[i];
array[i] = array[j]; array[i] = array[j];
array[j] = temp; array[j] = temp;
@ -127,7 +131,7 @@ class Geracao {
* Retorna o timestamp atual * Retorna o timestamp atual
* @return timestamp atual * @return timestamp atual
*/ */
public static long now(){ public long now(){
return new Date().getTime(); return new Date().getTime();
} }
@ -136,7 +140,7 @@ class Geracao {
* Retorna verdadeiro/falso indicando se o array esta ordenado * Retorna verdadeiro/falso indicando se o array esta ordenado
* @return boolean indicando se o array esta ordenado * @return boolean indicando se o array esta ordenado
*/ */
public static boolean isOrdenado(){ public boolean isOrdenado(){
boolean resp = true; boolean resp = true;
for(int i = 1; i < n; i++){ for(int i = 1; i < n; i++){
if(array[i] < array[i-1]){ if(array[i] < array[i-1]){

View File

@ -1,61 +1,33 @@
/** /**
* Algoritmo de ordenacao Heapsort * Algoritmo de ordenacao Heapsort
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
class Heapsort extends Geracao { class Heapsort extends Geracao {
/** /**
* Construtor. * Construtor.
*/ */
public Heapsort(){ public Heapsort(){
super(); super();
} }
/** /**
* Construtor. * Construtor.
* @param int tamanho do array de numeros inteiros. * @param int tamanho do array de numeros inteiros.
*/ */
public Heapsort(int tamanho){ public Heapsort(int tamanho){
super(tamanho); super(tamanho);
} }
public static void constroi(int tamHeap){ /**
for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){ * Algoritmo de ordenacao Heapsort.
swap(i, i/2); */
} @Override
} public void sort() {
public static void reconstroi(int tamHeap){
int i = 1;
while(i <= (tamHeap/2)){
int filho = getMaiorFilho(i, tamHeap);
if(array[i] < array[filho]){
swap(i, filho);
i = filho;
}else{
i = tamHeap;
}
}
}
public static int getMaiorFilho(int i, int tamHeap){
int filho;
if (2*i == tamHeap || array[2*i] > array[2*i+1]){
filho = 2*i;
} else {
filho = 2*i + 1;
}
return filho;
}
/**
* Algoritmo de ordenacao Heapsort.
*/
public static void heapsort() {
//Alterar o vetor ignorando a posicao zero //Alterar o vetor ignorando a posicao zero
int[] tmp = new int[n+1]; int[] tmp = new int[n+1];
for(int i = 0; i < n; i++){ for(int i = 0; i < n; i++){
@ -84,17 +56,33 @@ class Heapsort extends Geracao {
} }
public static void main(String[] args) { public void constroi(int tamHeap){
Heapsort heapsort = new Heapsort(200); for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){
heapsort.aleatorio(); swap(i, i/2);
heapsort.mostrar(); }
}
long comeco = now();
heapsort.heapsort();
long fim = now(); public void reconstroi(int tamHeap){
int i = 1;
//heapsort.mostrar(); while(i <= (tamHeap/2)){
System.out.println("Ordenado: " + heapsort.isOrdenado()); int filho = getMaiorFilho(i, tamHeap);
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s."); if(array[i] < array[filho]){
} swap(i, filho);
i = filho;
}else{
i = tamHeap;
}
}
}
public int getMaiorFilho(int i, int tamHeap){
int filho;
if (2*i == tamHeap || array[2*i] > array[2*i+1]){
filho = 2*i;
} else {
filho = 2*i + 1;
}
return filho;
}
} }

View File

@ -1,7 +1,7 @@
/** /**
* Algoritmo de ordenacao por insercao * Algoritmo de ordenacao por insercao
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 01/2020
*/ */
class Insercao extends Geracao { class Insercao extends Geracao {
@ -26,7 +26,8 @@ class Insercao extends Geracao {
/** /**
* Algoritmo de ordenacao por insercao. * Algoritmo de ordenacao por insercao.
*/ */
public static void insercao() { @Override
public void sort() {
for (int i = 1; i < n; i++) { for (int i = 1; i < n; i++) {
int tmp = array[i]; int tmp = array[i];
int j = i - 1; int j = i - 1;
@ -38,18 +39,4 @@ class Insercao extends Geracao {
array[j + 1] = tmp; array[j + 1] = tmp;
} }
} }
public static void main(String[] args) {
Insercao insercao = new Insercao(1000000);
insercao.aleatorio();
//insercao.mostrar();
long comeco = now();
insercao.insercao();
long fim = now();
//insercao.mostrar();
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s.");
}
} }

View File

@ -0,0 +1,39 @@
/**
* Algoritmo de ordenacao Mergesort
* @author Max do Val Machado
* @version 3 08/2020
*/
class Mergesort extends Geracao {
/**
* Construtor.
*/
public Mergesort(){
super();
}
/**
* Construtor.
* @param int tamanho do array de numeros inteiros.
*/
public Mergesort(int tamanho){
super(tamanho);
}
/**
* Algoritmo de ordenacao Mergesort.
*/
@Override
public void sort() {
}
/**
* Algoritmo de ordenacao Mergesort.
* @param int esq inicio do array a ser ordenado
* @param int dir fim do array a ser ordenado
*/
private void mergesort(int esq, int dir) {
}
}

View File

@ -1,10 +1,10 @@
/** /**
* Classe Principal * Classe Principal
* @author Max do Val Machado * @author Max do Val Machado
* @version 3 02/2020 * @version 3 08/2020
*/ */
class Principal { class Principal {
public static void main(String[] args) throws Exception { public static void main(String[] args) {
//Delcaracao de variaveis //Delcaracao de variaveis
Geracao algoritmo; Geracao algoritmo;

View File

@ -1,7 +1,7 @@
/** /**
* Algoritmo de ordenacao Quicksort * Algoritmo de ordenacao Quicksort
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
class Quicksort extends Geracao { class Quicksort extends Geracao {
@ -25,7 +25,8 @@ class Quicksort extends Geracao {
/** /**
* Algoritmo de ordenacao Quicksort. * Algoritmo de ordenacao Quicksort.
*/ */
public static void quicksort() { @Override
public void sort() {
quicksort(0, n-1); quicksort(0, n-1);
} }
@ -34,7 +35,7 @@ class Quicksort extends Geracao {
* @param int esq inicio do array a ser ordenado * @param int esq inicio do array a ser ordenado
* @param int dir fim do array a ser ordenado * @param int dir fim do array a ser ordenado
*/ */
private static void quicksort(int esq, int dir) { private void quicksort(int esq, int dir) {
int i = esq, j = dir; int i = esq, j = dir;
int pivo = array[(dir+esq)/2]; int pivo = array[(dir+esq)/2];
while (i <= j) { while (i <= j) {
@ -49,18 +50,4 @@ class Quicksort extends Geracao {
if (esq < j) quicksort(esq, j); if (esq < j) quicksort(esq, j);
if (i < dir) quicksort(i, dir); if (i < dir) quicksort(i, dir);
} }
public static void main(String[] args) throws Exception {
Quicksort quicksort = new Quicksort(10000000);
quicksort.aleatorio();
//quicksort.mostrar();
long comeco = now();
quicksort.quicksort();
long fim = now();
//quicksort.mostrar();
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s.");
}
} }

View File

@ -1,7 +1,7 @@
/** /**
* Algoritmo de ordenacao por selecao * Algoritmo de ordenacao por selecao
* @author Max do Val Machado * @author Max do Val Machado
* @version 3 02/2020 * @version 3 08/2020
*/ */
class Selecao extends Geracao { class Selecao extends Geracao {

View File

@ -1,7 +1,7 @@
/** /**
* Algoritmo de ordenacao Shellsort * Algoritmo de ordenacao Shellsort
* @author Max do Val Machado * @author Max do Val Machado
* @version 2 01/2015 * @version 3 08/2020
*/ */
class Shellsort extends Geracao { class Shellsort extends Geracao {
@ -26,7 +26,8 @@ class Shellsort extends Geracao {
/** /**
* Algoritmo de ordenacao Shellsort. * Algoritmo de ordenacao Shellsort.
*/ */
public static void shellsort() { @Override
public void sort() {
int h = 1; int h = 1;
do { h = (h * 3) + 1; } while (h < n); do { h = (h * 3) + 1; } while (h < n);
@ -45,7 +46,7 @@ class Shellsort extends Geracao {
* @param int cor cor do pseudo array. * @param int cor cor do pseudo array.
* @param int h passo do shelsort * @param int h passo do shelsort
*/ */
public static void insercaoPorCor(int cor, int h){ public void insercaoPorCor(int cor, int h){
for (int i = (h + cor); i < n; i+=h) { for (int i = (h + cor); i < n; i+=h) {
int tmp = array[i]; int tmp = array[i];
int j = i - h; int j = i - h;
@ -56,18 +57,4 @@ class Shellsort extends Geracao {
array[j + h] = tmp; array[j + h] = tmp;
} }
} }
public static void main(String[] args) {
Shellsort shellsort = new Shellsort(100000000);
shellsort.aleatorio();
//shellsort.mostrar();
long comeco = now();
shellsort.shellsort();
long fim = now();
//shellsort.mostrar();
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s.");
}
} }