Skip to content

Instantly share code, notes, and snippets.

@divanibarbosa
Last active November 30, 2023 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divanibarbosa/cd12abaa4034714c9803e83c0d1d1bd8 to your computer and use it in GitHub Desktop.
Save divanibarbosa/cd12abaa4034714c9803e83c0d1d1bd8 to your computer and use it in GitHub Desktop.
Ordenação BubbleSort em C
// Criado por: profa. Divani Barbosa Gavinier
// Curriculo Lattes: http://lattes.cnpq.br/8503400830635447
// divanibarbosa@gmail.com
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define n 10000
int v[n];
void BubbleSort() {
int troca;
for (int i = 0; i < n; i++)
for (int j = 0; j < n-1; j++)
if (v[j] > v[j+1]) {
troca = v[j];
v[j] = v[j+1];
v[j+1] = troca;
}
}
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
BubbleSort();
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 BubbleSort: %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