Skip to content

Instantly share code, notes, and snippets.

@divanibarbosa
Created November 30, 2023 01:39
Show Gist options
  • Save divanibarbosa/a863039c1f76d19c199eb0d18d40c30e to your computer and use it in GitHub Desktop.
Save divanibarbosa/a863039c1f76d19c199eb0d18d40c30e to your computer and use it in GitHub Desktop.
Ordenação InsertionSort em C
// Criado por: profa. Divani Barbosa Gavinier
// Curriculo Lattes: http://lattes.cnpq.br/8503400830635447
// [email protected]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define n 10000
int v[n];
void InsertionSort() {
int i, j;
int marcada;
for (i=1; i < n; i++) {
marcada = v[i];
j = i;
while ((j > 0) && (v[j-1] > marcada))
{
v[j] = v[j-1];
j = j - 1;
}
v[j] = marcada;
}
}
int main() {
int i;
clock_t inicio, fim; /* variáveis inicio e fim do tipo clock_t usadas em contagem de tempo */
srand(time(NULL));
printf("Conteudo vetor de %d itens:\n",n);
for(i=0; i<n; i++) {
v[i] = rand()%1000;
printf("%d ",v[i]);
}
inicio = clock(); // inicia contagem do tempo
InsertionSort();
fim = clock(); // finaliza a contagem de tempo
printf("\nConteudo vetor depois ordenacao:\n");
for(i=0; i<n; i++) {
printf("%d ",v[i]);
}
printf("\nTempo gasto ordenacao InsertionSort: %f segundos.\n\n",(float)(fim - inicio)/CLOCKS_PER_SEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment