Skip to content
Snippets Groups Projects
Commit 62c86c40 authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

HTML feito

parent 32151283
No related branches found
No related tags found
No related merge requests found
//Arquivo que contém os defines e os registros
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#define TAM 20 //tamanho do vetor
#define NUMMAX 50 //constante que tem o máximo de valores
//Cabealhos das Funes
void LerDados (int *Vetor);
void GeraVetor (int *Vetor);
void ImprimeVetor (int *Vetor);
void TempoSelectSort (int *Vetor);
void TempoBubbleSort (int *Vetor);
void TempoQuickSortRecursivo (int *Vetor);
void TempoQuickSortIterativo (int *Vetor);
void SelectSort (int *Vetor);
void BubbleSort (int *Vetor);
void QuickSortRecursivo (int Vetor[], int Esquerda, int Direita);
void QuickSortIterativo (int Vetor[], int Esquerda, int Direita);
void VerificaNumero (int *Vetor, int *Escolhidos);
void PesquisaBinaria (int *Vetor, int Elem);
#include "Estruturas.h"
#include "Funes.h"
int main () {
int Vetor[TAM], Escolhidos[4];
int comando, Ordena;
for (;;) {
printf ("1 - Jogar\n2 - Comparar Algortimos\n");
scanf ("%d", &comando);
switch (comando) {
case 1:
printf ("Digite 4 Valores de 1 a 50\n");
LerDados (Escolhidos);
GeraVetor (Vetor);
//Sorteia o Algoritmo para Ordenar o Vetor
srand (time (NULL));
Ordena = rand() % 4;
switch (Ordena) {
case 0:
printf ("O Vetor Foi Ordenado Pelo SelectSort\n");
SelectSort (Vetor);
break;
case 1:
printf ("O Vetor Foi Ordenado Pelo BubbleSort\n");
BubbleSort (Vetor);
break;
case 2:
printf ("O Vetor Foi Ordenado Pelo QuickSort Recursivo\n");
QuickSortRecursivo (Vetor, 0, TAM);
break;
case 3:
printf ("O Vetor Foi Ordenado Pelo QuickSort Iterativo\n");
QuickSortIterativo (Vetor, 0, TAM);
break;
}
//Chama a Pesquisa
VerificaNumero (Vetor, Escolhidos);
ImprimeVetor (Vetor);
break;
//Tempo de Execuo dos programas
case 2:
TempoSelectSort (Vetor);
TempoBubbleSort (Vetor);
TempoQuickSortRecursivo (Vetor);
TempoQuickSortIterativo (Vetor);
}
}
}
#include "Funes.h"
#include "Estruturas.h"
//Calcula o Tempo da Execuo do SelectSort
void TempoSelectSort (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
SelectSort (Vetor);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O SelectSort Demorou %d Centesimos\n", tmili);
}
//Calcula o Tempo da Execuo do BubbleSort
void TempoBubbleSort (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
BubbleSort (Vetor);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O BubbleSort Demorou %d Centesimos\n", tmili);
}
//Calcula o Tempo da Execuo do QuickSort Recursivo
void TempoQuickSortRecursivo (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
QuickSortRecursivo (Vetor, 0, TAM);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O QuickSort Recursivo Demorou %d Centesimos\n", tmili);
}
//Calcula o Tempo da Execuo do QuickSort Iterativo
void TempoQuickSortIterativo (int *Vetor) {
struct timeval inicio, final;
int tmili, i;
gettimeofday(&inicio, NULL);
for (i = 0; i < 10000; i ++) {
GeraVetor(Vetor);
QuickSortIterativo (Vetor, 0, TAM);
}
gettimeofday(&final, NULL);
tmili = (int) (1000 * (final.tv_sec - inicio.tv_sec) + (final.tv_usec - inicio.tv_usec) / 1000);
printf ("O QuickSort Iterativo Demorou %d Centesimos\n", tmili);
}
//Arquivo com os Algoritmos de Ordenao
#include "Estruturas.h"
//Funo para trocar valores
void Troca (int *Vetor, int i, int j) {
int aux;
aux = Vetor[i];
Vetor [i] = Vetor[j];
Vetor [j] = aux;
}
//Algoritmo de Ordenao do SelectSort
void SelectSort (int *Vetor) {
int i, j, menor;
for (i = 0; i < TAM - 1; i++) {
menor = i;
for (j = i + 1; j < TAM; j++)
if (Vetor[menor] > Vetor[j])
menor = j;
Troca (Vetor, i, menor);
}
}
//Algoritmo de Ordenao do BubbleSort
void BubbleSort (int *Vetor) {
bool trocou;
int i;
trocou = true;
while (trocou) {
trocou = false;
for (i=0; i < TAM; i++) {
if (Vetor[i] > Vetor[i+1]) {
Troca(Vetor,i,i+1);
trocou = true;
}
}
}
}
//Acha a posicao da Mediana
int Mediana(int *Vetor, int esq, int dir) {
int aux;
aux = (dir+esq)/2; // elem. do meio do vetor
//mediana dos 3 numeros
if ( (Vetor[esq] >= Vetor[aux]) && (Vetor[esq] <= Vetor[dir]) )
return esq;
else if ( (Vetor[esq] >= Vetor[dir]) && (Vetor[esq] <= Vetor[aux]) )
return esq;
else if ( (Vetor[dir] >= Vetor[aux]) && (Vetor[dir] <= Vetor[esq]) )
return dir;
else if ( (Vetor[dir] >= Vetor[esq]) && (Vetor[dir] <= Vetor[aux]) )
return dir;
else if ( (Vetor[aux] >= Vetor[esq]) && (Vetor[aux] <= Vetor[dir]) )
return aux;
else if ( (Vetor[aux] >= Vetor[dir]) && (Vetor[aux] <= Vetor[esq]) )
return aux;
}
//Particiona o QuickSort
int Particao (int *Vetor, int esq, int dir) {
int i,j,pivo,aux;
aux = Mediana(Vetor,esq,dir);
pivo = Vetor[aux];
i = esq;
j = dir;
while (i < j) {
while ( (Vetor[i] <= pivo) && (i < dir) )
i++;
while (Vetor[j] > pivo)
j--;
if (i < j)
Troca(Vetor,j,i);
}
Vetor[esq] = Vetor[j];
Vetor[j] = pivo;
return j;
}
//Algoritmo de Ordenao do QuickSort com Recurso
void QuickSortRecursivo (int Vetor[], int Esquerda, int Direita) {
int pospivo;
if (Esquerda < Direita) {
pospivo = Particao(Vetor, Esquerda, Direita);
QuickSortRecursivo(Vetor,Esquerda,pospivo-1);
QuickSortRecursivo(Vetor,pospivo+1,Direita);
}
}
//Algoritmo de Ordenao do QuickSort Iterativo
void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) {
int Posicao;
int Pilha[TAM];
int top;
top = -1;
//Empilha os Indices
Pilha[++top] = Esquerda;
Pilha[++top] = Direita;
while (top >= 0) {
//Desempilha os Indices
Direita = Pilha[top--];
Esquerda = Pilha[top--];
Posicao = Particao(Vetor, Esquerda, Direita);
if (Posicao-1 > Esquerda) {
//Empilha os Indices
Pilha[++top] = Esquerda;
Pilha[++top] = Posicao - 1;
}
if (Posicao+1 < Direita) {
//Empilha os Indices
Pilha[++top] = Posicao + 1;
Pilha[++top] = Direita;
}
}
}
//Arquivo com os algoritmos de busca
#include "Estruturas.h"
//Algoritmo de Pesquisa Binaria
int PesquisaBinaria (int *Vetor, int Elem) {
int meio, esquerda, direita;
esquerda = 0;
direita = TAM - 1;
do {
meio = (esquerda + direita)/2;
if (Elem > Vetor[meio])
esquerda = meio + 1;
else
direita = meio - 1;
} while (Elem != Vetor[meio] && esquerda <= direita);
if (Elem == Vetor [meio])
return (meio);
else
return -1; //return -1 pq o se o elemento estiver na primeira posição irá retornar 0 (posição que ele se encontra no vetor)
}
//Algoritmo de Pesquisa Sequencial
int PesquisaSequencial(int *Vetor, int Elem) {
int i;
for (i = 0; i < TAM; i++) {
if (Vetor[i] == Elem)
return i;
}
return -1; //-1 pq o elemento pode estar na posição zero
}
//Chama as funções de Pesqusia para procurar o número no vetor
void VerificaNumero (int *Vetor, int *Escolhidos) {
int i, acertos;
acertos = 0;
//os dois primeiros numeros serão buscados pela PesquisaSequencial
for (i = 0; i < 2; i++)
if (PesquisaSequencial (Vetor, Escolhidos[i]) != -1) {
printf ("Acertou o Numero %d, Parabéns\n", Escolhidos[i]);
acertos++;
}
//os dois ultimos numeros serão buscados pela PesquisaBinaria
for (i = i; i < 4; i++)
if (PesquisaBinaria (Vetor, Escolhidos[i]) != -1) {
printf ("Acertou o Numero %d, Parabéns\n", Escolhidos[i]);
acertos++;
}
printf ("O Total de Acertos foi de: %d\n", acertos);
}
#include "Estruturas.h"
//L os 4 nmeros que o usuario entra
void LerDados (int *Vetor) {
int i;
printf ("Digite 4 nmeros: ");
for (i = 0; i < 4; i++)
scanf ("%d", &Vetor[i]);
}
//Funo que gera valores aleatorios para o vetor
void GeraVetor (int *Vetor) {
int i;
srand (time(NULL));
for (i=0; i < TAM; i++)
Vetor[i] = (rand () % NUMMAX) + 1;
}
//Funo que imprime o vetor
void ImprimeVetor (int *Vetor) {
int i;
printf ("Pos: ");
for (i=0; i < TAM; i++)
printf ("\t%d", i);
printf ("\n");
printf ("Num: ");
for (i=0; i < TAM; i++)
printf ("\t%d", Vetor[i]);
printf ("\n");
}
......@@ -9,16 +9,19 @@
Fernando Claudecir Erd (fce15)</p>
Gabriel Ruschel Castanhel (grc15) </p>
<h3>Arquivos do Programa</h3>
<h5><a href="Estruturas.h.txt">Estruturas.h</a></h5>
<h5><a href="Ordenao.c.txt">Ordenao.c</a></h5>
<h5><a href="Pesquisa.c.txt">Pesquisa.c</a></h5>
<h5><a href="Vetor.c.txt">Vetor.c</a></h5>
<h5><a href="Main.c.txt">Main.c</a></h5>
<h5><a href="Estruturas.txt">Estruturas.h</a></h5>
<h5><a href="Funes.txt">Funes.h</a></h5>
<h5><a href="Ordenao.txt">Ordenao.c</a></h5>
<h5><a href="MedirTempo.txt">MedirTempo.c</a></h5>
<h5><a href="Pesquisa.txt">Pesquisa.c</a></h5>
<h5><a href="Vetor.txt">Vetor.c</a></h5>
<h5><a href="Main.txt">Main.c</a></h5>
<h5><a href="makefile.txt">Comandos de compilao</a></h5>
<h3>Relatrio</h3>
<h5><a href="Relatorio.html">Relatrio</a></h5>
<h3>Logs</h3>
<h5><a href="log.txt">Logs</a></h5>
<h5><a href="logMega.txt">Log Mega Quadra</a></h5>
<h5><a href="logCompare.txt">Log Comparao</a></h5>
</body>
</html>
//ATENÇÃO: NO LOG DAS COMPARAÇÕES FOI ALTERADO O TAMANHO DO VETOR NO ARQUIVO ESTRUTURAS.H (#DEFINE TAM) PARA QUE SE POSSA FAZER UMA ANALISE MAIS AMPLA DE COMO O TEMPO DA ORDENAÇÃO CRESCE DE ACORDO COM O TAMANHO DO VETOR
//No menu principal sempre foi escolhida a opção 2
1 - Jogar
2 - Comparar Algortimos
//Definindo #define TAM 10 - Tamanho do Vetor será 10
O SelectSort Demorou 28 Centesimos
O BubbleSort Demorou 31 Centesimos
O QuickSort Recursivo Demorou 28 Centesimos
O QuickSort Iterativo Demorou 27 Centesimos
//Definindo #define TAM 25 - Tamanho do Vetor será 25
O SelectSort Demorou 51 Centesimos
O BubbleSort Demorou 73 Centesimos
O QuickSort Recursivo Demorou 42 Centesimos
O QuickSort Iterativo Demorou 41 Centesimos
//Definindo #define TAM 50 - Tamanho do Vetor será 50
O SelectSort Demorou 109 Centesimos
O BubbleSort Demorou 249 Centesimos
O QuickSort Recursivo Demorou 79 Centesimos
O QuickSort Iterativo Demorou 77 Centesimos
//Definindo #define TAM 75 - Tamanho do Vetor será 75
O SelectSort Demorou 192 Centesimos
O BubbleSort Demorou 484 Centesimos
O QuickSort Recursivo Demorou 117 Centesimos
O QuickSort Iterativo Demorou 107 Centesimos
//Definindo #define TAM 100 - Tamanho do Vetor será 100
O SelectSort Demorou 296 Centesimos
O BubbleSort Demorou 824 Centesimos
O QuickSort Recursivo Demorou 154 Centesimos
O QuickSort Iterativo Demorou 139 Centesimos
//Definindo #define TAM 500 - Tamanho do Vetor será 500
O SelectSort Demorou 5370 Centesimos
O BubbleSort Demorou 23400 Centesimos
O QuickSort Recursivo Demorou 853 Centesimos
O QuickSort Iterativo Demorou 822 Centesimos
//Definindo #define TAM 1000 - Tamanho do Vetor será 1000
O SelectSort Demorou 20553 Centesimos //20 segundos
O BubbleSort Demorou 93153 Centesimos //93 segundos
O QuickSort Recursivo Demorou 1830 Centesimos
O QuickSort Iterativo Demorou 1782 Centesimos
1 - Jogar
2 - Comparar Algortimos
Entrada = 1
Digite 4 Valores de 1 a 50
Digite 4 números: 1 3 43 23
O Vetor Foi Ordenado Pelo SelectSort
Acertou o Numero 3, Parabéns
Acertou o Numero 23, Parabéns
O Total de Acertos foi de: 2
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Num: 2 3 3 4 12 14 17 20 21 23 31 33 33 35 36 40 41 41 46 49
1 - Jogar
2 - Comparar Algortimos
Entrada = 1
Digite 4 Valores de 1 a 50
Digite 4 números: 7 11 25 40
O Vetor Foi Ordenado Pelo QuickSort Recursivo
Acertou o Numero 11, Parabéns
O Total de Acertos foi de: 1
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Num: 10 11 13 13 13 15 15 17 18 20 20 26 26 30 31 31 46 47 47 49
1 - Jogar
2 - Comparar Algortimos
Entrada = 1
Digite 4 Valores de 1 a 50
Digite 4 números: 10 23 35 38
O Vetor Foi Ordenado Pelo QuickSort Iterativo
O Total de Acertos foi de: 0
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Num: 6 6 7 7 8 17 18 18 20 25 27 33 42 42 42 42 48 48 50 50
1 - Jogar
2 - Comparar Algortimos
Entrada = 1
Digite 4 Valores de 1 a 50
Digite 4 números: 1 32 2 4
O Vetor Foi Ordenado Pelo BubbleSort
Acertou o Numero 1, Parabéns
Acertou o Numero 2, Parabéns
Acertou o Numero 4, Parabéns
O Total de Acertos foi de: 3
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Num: 1 1 2 4 9 11 11 12 13 25 31 38 40 41 44 45 47 49 49 49
all:
gcc -c Vetor.c
gcc -c Ordenação.c
gcc -c Pesquisa.c
gcc -c MedirTempo.c
gcc -c Main.c
gcc Vetor.o Ordenação.o Main.o Pesquisa.o MedirTempo.o -o Loteria -g
rm *.o
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment