-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsercao1.c
More file actions
35 lines (34 loc) · 1.06 KB
/
Copy pathinsercao1.c
File metadata and controls
35 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void
insercao (long int* contador, int n, int vetor[])
{
int i, j, x;
for (j = 1; j < n; ++j) {
x = vetor[j];
for (i = j-1; i >= 0 && vetor[i] > x; --i)
{vetor[i+1] = vetor[i]; (*contador)++;}
vetor[i+1] = x;
}
}
int main ()
{
int tam = 100000; //tamanho da input ("n")
int vetor[tam];
long int custo;
clock_t start, end; //variavel que representa o numero de iteraçoes do "for"; Obs.: para n = 100000, foi preciso alterar pra long int.
double tempo;
for (int m = 0; m < 10; m++) //realiza as 10 iterações necessárias
{
start = 0; end = 0.0; tempo = 0.0; custo = 0;
srand(rand() % 10000); //reseta a sequencia aleatoria
for (int i = 0; i < tam; i++) //atribui valores aleatorios
vetor[i] = rand() % tam; //ao vetor de entrada
start = clock();
insercao(&custo, tam, vetor);
end = clock();
tempo = ((double) end - start) / CLOCKS_PER_SEC;
printf ("%f\n", tempo);
}
}