//================================================================
// Nome Do Arquivo: selection.c
// File Name: selection.c
//
// Descrição: Implementação do algoritmo de ordenação por seleção
// Description: Selection sort Algorithm
//================================================================
// Libs
#include <stdio.h>
#include <stdlib.h>
// Define uma constante
// Define a constant
#define MAX 10
// Protótipo da função de ordenação
// Ordination function prototype
void selection_sort(int *a);
// Função main
// Main Function
int main(int argc, char** argv)
{
int i, vet[MAX];
// Lê MAX ou 10 valores
// Read MAX or 10 values
for(i = 0; i < MAX; i++)
{
printf("Digite um valor: ");
scanf("%d", &vet[i]);
}
// Ordena os valores
// Order values
selection_sort(vet);
// Imprime os valores ordenados
// Print values in order ascendant
printf("nnValores ordenadosn");
for(i = 0; i < MAX; i++)
{
printf("%dn", vet[i]);
}
system("pause");
return 0;
}
// Função de Ordenação por Seleção
// Selection sort function
void selection_sort(int *a)
{
int i, j, k, tmp, troca;
for(i = 0; i < MAX-1; i++)
{
troca = 0;
k = i;
tmp = a[i];
for(j = i+1; j < MAX; j++)
{
if(a[j] < tmp)
{
k = j;
tmp = a[j];
troca = 1;
}
}
if(troca)
{
a[k] = a[i];
a[i] = tmp;
}
}
}
Algoritmo de Ordenação por Seleção em C (Selection Sort)
Deixe um comentário