Skip to content

Commit

Permalink
Commit inicial
Browse files Browse the repository at this point in the history
  • Loading branch information
megalemarcelo committed Jun 8, 2022
0 parents commit 574622e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ordena.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ordena.h"

void gera_vetor (int *v, int n) {
int i;
for (i = 0; i<n; i++) {
v[i] = rand();
}
}

void bubble (int v[], int n) {
int i, j, aux;
for (i=1; i<n; i++) {
for (j=0; j<n-i; j++) {
if (v[j] > v[j+1]) {
aux = v[j];
v[j] = v[j+1];
v[j+1] = aux;
}
}
}
}

void insertion (int v[], int n){
int i, j, chave;
for (i = 1; i < n; i++){
chave = v[i];
j = i - 1;
while (j >= 0 && v[j] > chave){
v[j + 1] = v[j];
j--;
}
v[j + 1] = chave;
}
}
5 changes: 5 additions & 0 deletions ordena.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void gera_vetor(int*, int);

void bubble (int*, int);

void insertion (int*, int);
46 changes: 46 additions & 0 deletions teste_ordena.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main () {
int *v, n;
unsigned long int ini, fim;

printf("\nDigite o tamanho do vetor: ");
scanf("%d", &n);

//BUBBLE
v = (int *) malloc (n * sizeof(int));

srand(time(0));
gera_vetor(v,n);

ini = time(0);
bubble(v,n);
fim = time(0);

system("cls");

printf("\nOrdenacao do vetor de tamanho %d com Bubble\nTempo de execucao: %ld segundos", n, fim-ini);

free(v);

fim=0;
ini=0;

//INSERTION
v = (int *) malloc (n * sizeof(int));

srand(time(0));
gera_vetor(v,n);

ini = time(0);
insertion(v,n);
fim = time(0);

printf("\n\nOrdenacao do vetor de tamanho %d com Insertion\nTempo de execucao: %ld segundos\n", n, fim-ini);

free(v);

return 0;
}

0 comments on commit 574622e

Please sign in to comment.