Created
April 20, 2012 12:33
-
-
Save eduardodx/2428208 to your computer and use it in GitHub Desktop.
Selection Sort
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
/** | |
* http://pt.wikipedia.org/wiki/Selection_sort | |
*/ | |
#include <stdio.h> | |
#define VL 5 // Tamanho do vetor | |
int main () { | |
int v[6], indice, min, i, j; | |
// Ler o vetor | |
for (i = 0; i < VL; i++) { | |
scanf("%d", &v[i]); | |
} | |
// Selection Sort | |
for (j = 0; j < VL; j++) { | |
// Ajustar variaveis para cada passo da varredura | |
min = v[j]; | |
indice = j; | |
// Pegar o menor elemento | |
for (i = j; i < VL; i++) { | |
if (v[i] < min) { | |
min = v[i]; | |
indice = i; | |
} | |
} | |
// Trocar elementos de posicao | |
v[indice] = v[j]; | |
v[j] = min; | |
} | |
// Mostrar o vetor | |
for (i = 0; i < VL; i++) { | |
printf("%d\n", v[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment