ordenação bolha
This commit is contained in:
parent
1afbe8f831
commit
d8032351a2
|
|
@ -0,0 +1 @@
|
|||
*.exe
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue