View
10
Download
0
Category
Preview:
DESCRIPTION
MÉTODOS DE ORDENAÇÃOSELECTION SORT
Citation preview
SELECTION SORT
• ELISEU ZAPOTOSZEK
• MICHEL ANDREAS NIEWÖHNER
MÉTODOS DE ORDENAÇÃO
SELECTION SORT
(ORDENAÇÃO POR SELEÇÃO)
FUNCIONAMENTO DO SELECTION SORT Procuramos o menor valor do vetor e
colocamos ele em vetor[1].
Procuramos o menor valor do vetor excluindo o já colocado e colocamos ele em vetor[2].
E assim vamos indo até termos todo o vetor ordenado.
//exemplo de ordenação crescente de um vetor com dez posições:
INICIO
Inteiro a[10] <- {12, 7, 4, 50, 8, 15, 30, 21, 18, 1} //declarar as variáveis:
Inteiro i, j, aux, menor
Para i de 0 ate 8 passo 1//ordenar o vetor:
menor <- i
Para j De i+1 Até 9 passo 1
Se a[menor] > a[j] então
menor <- j
Fimse
Código em VisualG
Próximo Se menor =/= i então aux <- a[menor] a[menor] <- a[i] a[i] <- aux Fimse Próximo Para i De 0 Até 9 Passo 1 Escrever a[i], " "//escrever o vetor ordenado: escreva("O computador está pronto a ser utilizado") Próximo FIM
Código em VisualG
Código da ordenação SelectionSort com Strings void ordenar_selecao_nome() {
int i,j,n; for(i=0; i<n-1; i++) { for(j=i+1; j<n; j++) { if(strcmp(vetor[i], vetor[j])>0) { strcpy(aux_char, vetor[i]); strcpy(vetor[i], vetor[j]); strcpy(vetor[j], aux_char); } } }
Código em C#
public void SelectionSort(int[] vetor) { int min, aux; for (int i = 0; i < vetor.Length - 1; i++) { min = i; for (int j = i + 1; j < vetor.Length; j++) if (vetor[j] < vetor[min]) min = j; if (min != i) { aux = vetor[min]; vetor[min] = vetor[i]; vetor[i] = aux; } } }
Continuação Pascal
Program selectionsort(input,output);
Var
i,tam,a,tmp,n : integer;
v : array[1..50] of integer;
trocou : boolean;
Begin
tam:=n-1;
a:=1;
trocou:=true;
while (trocou) and (a<=tam) do
Begin
trocou:=false;
for i:=1 to tam do
if v[i]>v[i+1] then
Begin
tmp:=v[i];
v[i]:=v[i+1];
v[i+1]:=tmp;
trocou:=true;
End;
tam:=tam-1;
a:=a+1;
End;
Readln;
End.
Continuação Pascal
Código em Java
public static void selection(int[] array) {
for (int fixo = 0; fixo < array.length - 1; fixo++) {
int menor = fixo;
for (int i = menor + 1; i < array.length; i++){
if (array[i] < array[menor]){
menor = i;
}
}
if (menor != fixo) {
// Troca
array[fixo] += array[menor];
array[menor] = array[fixo] - array[menor];
array[fixo] -= array[menor];
} } }
Código em Visual Basic
Public Function SelectionSort(Vetor(), tam)
Dim i, j
Dim min, aux
For i = 0 To tam
min = i
For j = i + 1 To tam
If Vetor(j) < Vetor(min) Then min = j
Next j
If Vetor(i) <> Vetor(min) Then
aux = Vetor(i)
Vetor(i) = Vetor(min)
Vetor(min) = aux
End If
Next i
End Function
Código em Ruby
def selection(array) a = array min = first first.upto(a.size-1) do |i| if a[min] > a[i] min = i end end a[first], a[min] = a[min], a[first] end print a end
Código em Python
def selectsort (L): n=len(L) for i in range(n-1): mini = i for j in range(i+1,n): if(L[j]<L[mini]): mini=j L[i],L[mini]=L[mini],L[i]
<?php $array = array(3,2,6,7,1,0,8,9,4,5); //cria um array
desordenado print_r($array); //exibe o array desordenado na tela echo "<br /><br />"; //gera duas linhas em branco de
espaço for ($i = 0; $i < count($array); $i++) { //percorre o
array para ordenar $menor = $i; //define o primeiro índice como sendo o
menor for ($j = $i + 1; $j < count($array); $j++) {
//procura outro índice com valor menor anteriormente marcado
if ($array[$j] < $array[$menor]) { //compara se existe outro valor menor que o anteriormente marcado
Código em PHP
$menor = $j; //determina o índice do novo valor menor encontrado
} }
if ($menor != $i) {//compara se o índice definido no início foi modificado
$aux = $array[$i]; //realiza troca de valores conforme o índices
$array[$i] = $array[$menor];
$array[$menor] = $aux;
} }
echo "<br /><br />"; //gera duas linhas em branco apenas para dar espaço
print_r($array); //imprimi o array ordenadamente na tela
?>
Continuação PHP
Recommended