Adapta algoritmos de ordenação em Java para C
This commit is contained in:
parent
9b74306ba1
commit
a9d6ed1d6a
|
|
@ -1,6 +1,6 @@
|
||||||
#ifndef BOLHA_H
|
#ifndef BOLHA_H
|
||||||
#define BOLHA_H
|
#define BOLHA_H
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
#include "geracao.h"
|
#include "geracao.h"
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void bolha(int *array, int n){
|
void bolha(int *array, int n){
|
||||||
|
|
|
||||||
|
|
@ -28,29 +28,35 @@ int main(int argc, char **argv) {
|
||||||
comeco = clock();
|
comeco = clock();
|
||||||
bolha(array, n);
|
bolha(array, n);
|
||||||
fim = clock();
|
fim = clock();
|
||||||
total = (fim - comeco) / (double)CLOCKS_PER_SEC / 1000.0;
|
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
|
||||||
mostrar(array, n);
|
mostrar(array, n);
|
||||||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n));
|
printf("Tempo para ordenar: %f ms (%s).\n",
|
||||||
|
total,
|
||||||
|
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
|
||||||
|
|
||||||
printf("Teste Bolha: Ordem Decrescente\n");
|
printf("\nTeste Bolha: Ordem Decrescente\n");
|
||||||
decrescente(array, n);
|
decrescente(array, n);
|
||||||
mostrar(array, n);
|
mostrar(array, n);
|
||||||
comeco = clock();
|
comeco = clock();
|
||||||
bolha(array, n);
|
bolha(array, n);
|
||||||
fim = clock();
|
fim = clock();
|
||||||
total = (fim - comeco) / (double)CLOCKS_PER_SEC / 1000.0;
|
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
|
||||||
mostrar(array, n);
|
mostrar(array, n);
|
||||||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n));
|
printf("Tempo para ordenar: %f ms (%s).\n",
|
||||||
|
total,
|
||||||
|
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
|
||||||
|
|
||||||
printf("Teste Bolha: Ordem Aleatoria\n");
|
printf("\nTeste Bolha: Ordem Aleatoria\n");
|
||||||
aleatorio(array, n);
|
aleatorio(array, n);
|
||||||
mostrar(array, n);
|
mostrar(array, n);
|
||||||
comeco = clock();
|
comeco = clock();
|
||||||
bolha(array, n);
|
bolha(array, n);
|
||||||
fim = clock();
|
fim = clock();
|
||||||
total = (fim - comeco) / (double)CLOCKS_PER_SEC / 1000.0;
|
total = ((fim - comeco) / (double)CLOCKS_PER_SEC) * 1000.0;
|
||||||
mostrar(array, n);
|
mostrar(array, n);
|
||||||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n));
|
printf("Tempo para ordenar: %f ms (%s).\n",
|
||||||
|
total,
|
||||||
|
isOrdenado(array, n) ? "ORDENADO" : "DESORDENADO");
|
||||||
|
|
||||||
free(array);
|
free(array);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef COUNTINGSORT_H
|
||||||
|
#define COUNTINGSORT_H
|
||||||
|
//=============================================================================
|
||||||
|
#include "geracao.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++);
|
||||||
|
}
|
||||||
|
//=============================================================================
|
||||||
|
#endif
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef GERACAO_H
|
#ifndef GERACAO_H
|
||||||
#define GERACAO_H
|
#define GERACAO_H
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
#ifndef HEAPSORT_H
|
||||||
|
#define 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];
|
||||||
|
}
|
||||||
|
// array = tmp;
|
||||||
|
|
||||||
|
//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
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#ifndef INSERCAO_H
|
#ifndef INSERCAO_H
|
||||||
#define INSERCAO_H
|
#define INSERCAO_H
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
#include "geracao.h"
|
#include "geracao.h"
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void insercao(int *array, int n){
|
void insercao(int *array, int n){
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef MERGESORT_H
|
||||||
|
#define MERGESORT_H
|
||||||
|
//=============================================================================
|
||||||
|
#include "geracao.h"
|
||||||
|
//=============================================================================
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef QUICKSORT_H
|
||||||
|
#define QUICKSORT_H
|
||||||
|
//=============================================================================
|
||||||
|
#include "geracao.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);
|
||||||
|
}
|
||||||
|
//=============================================================================
|
||||||
|
#endif
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#ifndef SELECAO_H
|
#ifndef SELECAO_H
|
||||||
#define SELECAO_H
|
#define SELECAO_H
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
#include "geracao.h"
|
#include "geracao.h"
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void selecao(int *array, int n){
|
void selecao(int *array, int n){
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
#ifndef SHELLSORT_H
|
||||||
|
#define SHELLSORT_H
|
||||||
|
//=============================================================================
|
||||||
|
#include "geracao.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);
|
||||||
|
}
|
||||||
|
//=============================================================================
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue