forked from icei-pucminas/aeds2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1afbe8f
commit d803235
Showing
5 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.exe |
2 changes: 1 addition & 1 deletion
2
U4 - Ordenação/README.md → ... Ordenação em memória principal/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# U4 - Ordenação | ||
# U4 - Ordenação em memória principal | ||
Repositório de códigos da disciplina de Algoritmos e Estrutura de Dados II |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef BOLHA_H | ||
#define BOLHA_H | ||
//============================================================================= | ||
#include "ordenacao.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]); | ||
} | ||
} | ||
} | ||
} | ||
//============================================================================= | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#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); | ||
print_array(array, n); | ||
comeco = clock(); | ||
bolha(array, n); | ||
fim = clock(); | ||
total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; | ||
print_array(array, n); | ||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n)); | ||
|
||
printf("Teste Bolha: Ordem Decrescente\n"); | ||
decrescente(array, n); | ||
print_array(array, n); | ||
comeco = clock(); | ||
bolha(array, n); | ||
fim = clock(); | ||
total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; | ||
print_array(array, n); | ||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n)); | ||
|
||
printf("Teste Bolha: Ordem Aleatoria\n"); | ||
aleatorio(array, n); | ||
print_array(array, n); | ||
comeco = clock(); | ||
bolha(array, n); | ||
fim = clock(); | ||
total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; | ||
print_array(array, n); | ||
printf("Tempo para ordenar: %f ms (%i).", total, isOrdenado(array, n)); | ||
|
||
free(array); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef ORDENACAO_H | ||
#define ORDENACAO_H | ||
//============================================================================= | ||
#include <stdio.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <stdlib.h> | ||
#include <stdbool.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) { | ||
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) { | ||
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) { | ||
int i, pos; | ||
crescente(array, n); | ||
for (i = 0; i < n; i++) { | ||
pos = rand() % n; | ||
swap(&array[i], &array[pos]); | ||
} | ||
} | ||
//============================================================================= | ||
// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO arranjo | ||
void print_array(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){ | ||
int i; | ||
for(int i = 1; i < n; i++){ | ||
if(array[i-1] > array[i]){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
//============================================================================= | ||
#endif |