17
SELECTION SORT ELISEU ZAPOTOSZEK MICHEL ANDREAS NIEWÖHNER

Trabalho Select Sort

Embed Size (px)

DESCRIPTION

MÉTODOS DE ORDENAÇÃOSELECTION SORT

Citation preview

Page 1: Trabalho Select Sort

SELECTION SORT

• ELISEU ZAPOTOSZEK

• MICHEL ANDREAS NIEWÖHNER

Page 2: Trabalho Select Sort

MÉTODOS DE ORDENAÇÃO

SELECTION SORT

(ORDENAÇÃO POR SELEÇÃO)

Page 3: Trabalho Select Sort

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.

Page 4: Trabalho Select Sort
Page 5: Trabalho Select Sort
Page 6: Trabalho Select Sort

//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

Page 7: Trabalho Select Sort

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

Page 8: Trabalho Select Sort

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); } } }

Page 9: Trabalho Select Sort

 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; } } }

Page 10: Trabalho Select Sort

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

Page 11: Trabalho Select Sort

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

Page 12: Trabalho Select Sort

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];

} } }

Page 13: Trabalho Select Sort

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

Page 14: Trabalho Select Sort

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

Page 15: Trabalho Select Sort

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]

Page 16: Trabalho Select Sort

<?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

Page 17: Trabalho Select Sort

$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