ordenacao
This commit is contained in:
parent
25371e5a5a
commit
a0243a7254
|
|
@ -61,4 +61,4 @@ int main(int argc, char **argv) {
|
|||
free(array);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ void decrescente(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]);
|
||||
|
|
@ -61,4 +62,4 @@ bool isOrdenado(int *array, int n){
|
|||
return true;
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
#include "bolha.h"
|
||||
#include "countingsort.h"
|
||||
#include "heapsort.h"
|
||||
#include "insercao.h"
|
||||
#include "mergesort.h"
|
||||
#include "quicksort.h"
|
||||
#include "selecao.h"
|
||||
#include "shellsort.h"
|
||||
//=============================================================================
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
//Delcaracao de variaveis
|
||||
int n = (argc < 2) ? 1000 : atoi(argv[1]);
|
||||
int *array = (int*) malloc(n*sizeof(int));
|
||||
clock_t inicio, fim;
|
||||
double total;
|
||||
|
||||
|
||||
//Geracao do conjunto a ser ordenado
|
||||
aleatorio(array, n);
|
||||
//crescente(array, n);
|
||||
//decrescente(array, n);
|
||||
|
||||
|
||||
//Mostrar o conjunto a ser ordenado
|
||||
//mostrar(array, n);
|
||||
|
||||
|
||||
//Execucao do algoritmo de ordenacao
|
||||
inicio = clock();
|
||||
//bolha(array, n);
|
||||
//countingsort(array, n);
|
||||
//heapsort(array, n);
|
||||
//insercao(array, n);
|
||||
//mergesort(array, n);
|
||||
//quicksort(array, n);
|
||||
selecao(array, n);
|
||||
//shellsort(array, n);
|
||||
fim = clock();
|
||||
total = ((fim - inicio) / (double)CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
//Mostrar o conjunto ordenado, tempo de execucao e status da ordenacao
|
||||
//algoritmo.mostrar(array, n);
|
||||
printf("Tempo para ordenar: %f s.\n", total);
|
||||
printf("isOrdenado: %s\n", isOrdenado(array, n) ? "true" : "false");
|
||||
|
||||
//Desalocar o espaco de memoria do array
|
||||
free(array);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -4,10 +4,9 @@
|
|||
#include "geracao.h"
|
||||
//=============================================================================
|
||||
void selecao(int *array, int n){
|
||||
int i, j, menor;
|
||||
for (i = 0; i < (n - 1); i++) {
|
||||
menor = i;
|
||||
for (j = (i + 1); j < n; j++){
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -146,4 +146,11 @@ class Geracao {
|
|||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Metodo a ser implementado nas subclasses
|
||||
*/
|
||||
public void sort(){
|
||||
System.out.println("Método a ser implementado nas subclasses.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Classe Principal
|
||||
* @author Max do Val Machado
|
||||
* @version 3 02/2020
|
||||
*/
|
||||
class Principal {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
//Delcaracao de variaveis
|
||||
Geracao algoritmo;
|
||||
int n = (args.length < 1) ? 1000 : Integer.parseInt(args[0]);
|
||||
double inicio, fim;
|
||||
|
||||
|
||||
//Inicializacao do algoritmo de ordenacao
|
||||
//algoritmo = new Bolha(n);
|
||||
//algoritmo = new Countingsort(n);
|
||||
//algoritmo = new Heapsort(n);
|
||||
//algoritmo = new Insercao(n);
|
||||
//algoritmo = new Mergesort(n);
|
||||
//algoritmo = new Quicksort(n);
|
||||
algoritmo = new Selecao(n);
|
||||
//algoritmo = new Shellsort(n);
|
||||
|
||||
|
||||
//Geracao do conjunto a ser ordenado
|
||||
algoritmo.aleatorio();
|
||||
//algoritmo.crescente();
|
||||
//algoritmo.decrescente();
|
||||
|
||||
|
||||
//Mostrar o conjunto a ser ordenado
|
||||
//algoritmo.mostrar();
|
||||
|
||||
|
||||
//Execucao do algoritmo de ordenacao
|
||||
inicio = algoritmo.now();
|
||||
algoritmo.sort();
|
||||
fim = algoritmo.now();
|
||||
|
||||
|
||||
//Mostrar o conjunto ordenado, tempo de execucao e status da ordenacao
|
||||
//algoritmo.mostrar();
|
||||
System.out.println("Tempo para ordenar: " + (fim-inicio)/1000.0 + " s.");
|
||||
System.out.println("isOrdenado: " + algoritmo.isOrdenado());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* Algoritmo de ordenacao por selecao
|
||||
* @author Max do Val Machado
|
||||
* @version 2 01/2015
|
||||
* @version 3 02/2020
|
||||
*/
|
||||
|
||||
class Selecao extends Geracao {
|
||||
|
|
@ -26,7 +26,8 @@ class Selecao extends Geracao {
|
|||
/**
|
||||
* Algoritmo de ordenacao por selecao.
|
||||
*/
|
||||
public static void selecao() {
|
||||
@Override
|
||||
public void sort() {
|
||||
for (int i = 0; i < (n - 1); i++) {
|
||||
int menor = i;
|
||||
for (int j = (i + 1); j < n; j++){
|
||||
|
|
@ -37,18 +38,4 @@ class Selecao extends Geracao {
|
|||
swap(menor, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Selecao selecao = new Selecao(100);
|
||||
selecao.aleatorio();
|
||||
selecao.mostrar();
|
||||
|
||||
long comeco = now();
|
||||
selecao.selecao();
|
||||
long fim = now();
|
||||
|
||||
selecao.mostrar();
|
||||
System.out.println("Tempo para ordenar: " + (fim-comeco)/1000.0 + " s.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue