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

Estruturação

parent 6d4e962a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include "Estruturas.h" #include "Estruturas.h"
#include "Funções.h" #include "Funções.h"
//Calcula o Tempo da Execução 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 Execução 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 Execução 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 Execução 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);
}
int main () { int main () {
int Vetor[TAM], Escolhidos[4]; int Vetor[TAM], Escolhidos[4];
......
#include "Funções.h"
#include "Estruturas.h"
//Calcula o Tempo da Execução 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 Execução 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 Execução 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 Execução 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);
}
#include <stdio.h>
void Troca (int *Vetor, int i, int j) {
int aux;
aux = Vetor[i];
Vetor [i] = Vetor[j];
Vetor [j] = aux;
}
void printVetor( int Vetor[], int n ) {
int i;
for ( i = 0; i < n; ++i )
printf( "%d ", Vetor[i] );
printf ("\n");
}
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;
}
int Particao (int *Vetor, int esq, int dir) {
int i,j,pivo,aux;
aux = Mediana(Vetor,esq,dir);
pivo = Vetor[aux];
printf("%d\n",pivo);
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);
}
printVetor(Vetor,18);
Vetor[esq] = Vetor[j];
Vetor[j] = pivo;
return j;
}
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);
}
}
void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) {
int Posicao;
int Pilha[ Direita - Esquerda + 1 ];
int top;
top = -1;
Pilha[ ++top ] = Esquerda;
Pilha[ ++top ] = Direita;
while ( top >= 0 )
{
Direita = Pilha[ top-- ];
Esquerda = Pilha[ top-- ];
Posicao = Particao( Vetor, Esquerda, Direita );
if ( Posicao-1 > Esquerda )
{
Pilha[ ++top ] = Esquerda;
Pilha[ ++top ] = Posicao - 1;
}
if ( Posicao+1 < Direita )
{
Pilha[ ++top ] = Posicao + 1;
Pilha[ ++top ] = Direita;
}
}
}
int main() {
int Vetor[] = {1,2,3,4,5,8,6,4,43,43,56,23,43,4,3,75,342,532};
int n = sizeof( Vetor ) / sizeof( *Vetor );
printVetor (Vetor, n);
QuickSortIterativo( Vetor, 0, n - 1 );
printVetor( Vetor, n );
}
...@@ -2,7 +2,8 @@ all: ...@@ -2,7 +2,8 @@ all:
gcc -c Vetor.c gcc -c Vetor.c
gcc -c Ordenação.c gcc -c Ordenação.c
gcc -c Pesquisa.c gcc -c Pesquisa.c
gcc -c MedirTempo.c
gcc -c Main.c gcc -c Main.c
gcc Vetor.o Ordenação.o Main.o Pesquisa.o -o Loteria -g gcc Vetor.o Ordenação.o Main.o Pesquisa.o MedirTempo.o -o Loteria -g
rm *.o rm *.o
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment