-
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
0 parents
commit 574622e
Showing
3 changed files
with
85 additions
and
0 deletions.
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,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; | ||
} | ||
} |
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,5 @@ | ||
void gera_vetor(int*, int); | ||
|
||
void bubble (int*, int); | ||
|
||
void insertion (int*, int); |
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,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; | ||
} |