Skip to content

Instantly share code, notes, and snippets.

@divanibarbosa
Created November 30, 2023 01:36
Show Gist options
  • Save divanibarbosa/951a1bc560403f45096a2e071f821509 to your computer and use it in GitHub Desktop.
Save divanibarbosa/951a1bc560403f45096a2e071f821509 to your computer and use it in GitHub Desktop.
Ordenação SelectionSort 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 SelectionSort() {
int i, j, min;
int troca;
for (i=0; i < n-1; i++) {
min = i;
for (j=i+1; j < n; j++) {
if (v[j] < v[min]) {
min=j;
}
}
troca = v[i];
v[i] = v[min];
v[min] = 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
SelectionSort();
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 SelectionSort: %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