VisualG - 5 larger numbers

0

I'm getting 5 values and I need to sort from smallest to largest, without using loop or array.

I just managed to find out the smallest and largest; How could you find the ones in the middle? Here's what I've done so far:

Algoritmo "semnome"
// Disciplina  :  [Linguagem e Lógica de Programação] 
// Professor   : Antonio Carlos Nicolodi 
// Descrição   : Aqui você descreve o que o programa faz! (função)
// Autor(a)    : Nome do(a) aluno(a)
// Data atual  : 25/09/2017
Var
// Seção de Declarações das variáveis 
n1, n2, n3, n4, n5 : inteiro
maior, menor : inteiro
Inicio
// Seção de Comandos, procedimento, funções, operadores, etc... 


escreval("Numero 1:")
leia(n1)

escreval("Numero 2:")
leia(n2)

escreval("Numero 3:")
leia(n3)

escreval("Numero 4:")
leia(n4)

escreval("Numero 5:")
leia(n5)

se (n1 > n2) e (n1 > n3) e (n1 > n4) e (n1 > n5) entao
      maior <- n1
fimse

se (n2 > n1) e (n2 > n3) e (n2 > n4) e (n2 > n5) entao
      maior <- n2
fimse

se (n3 > n1) e (n3 > n2) e (n3 > n4) e (n3 > n5) entao
      maior <- n3
fimse

se (n4 > n1) e (n4 > n2) e (n4 > n3) e (n4 > n5) entao
      maior <- n4
fimse

se (n5 > n1) e (n5 > n2) e (n5 > n3) e (n5 > n4) entao
      maior <- n5
fimse

se (n1 < n2) e (n1 < n3) e (n1 < n4) e (n1 < n5) entao
      menor <- n1
fimse

se (n2 < n1) e (n2 < n3) e (n2 < n4) e (n2 < n5) entao
      menor <- n2
fimse

se (n3 < n1) e (n3 < n2) e (n3 < n4) e (n3 < n5) entao
      menor <- n3
fimse

se (n4 < n1) e (n4 < n2) e (n4 < n3) e (n4 < n5) entao
      menor <- n4
fimse

se (n5 < n1) e (n5 < n2) e (n5 < n3) e (n5 < n4) entao
      menor <- n5
fimse

escreval(menor)

escreval(maior)

Fimalgoritmo
    
asked by anonymous 25.09.2017 / 22:16

1 answer

2

Your writing is bad. It did not make clear what the expected result, nor what was his doubt.

I think you're wondering how to sort the variables n1 to n5 to print them. Right? Well, this is a great case to study ordering networks.

The more detailed explanation is in this answer . I will transcribe the most relevant passages:

  

A sort network consists of horizontal wires that represent the positions in the array, and also vertical bridges that bind wires. Each bridge connects only and exclusively two wires; in each bridge, the elements of the two connected wires are compared and, if necessary, replaced. It also has another interesting property in ordering networks: time goes from left to right, and a wire can only be on a bridge at the same time.

     

Inyourcase,eachpositionisdefinedbyavariable,notbyapositionofthevector.

Youcansearchforthemostoptimizedsortingnetworkforfivepositions,I'llgothroughthepseudocodefor8variableshere.NotealsothatIalreadyconsiderthatn0an7alreadycomeswiththevaluesfilledfromsomewhere.AlsonotethatIusepositionalindexingstyle c , so I'll start at 0 .

se n0 > n1:
    swp = n0
    n0 = n1
    n1 = swp
se n2 > n3:
    swp = n2
    n2 = n3
    n3 = swp
se n4 > n5:
    swp = n4
    n4 = n5
    n5 = swp
se n6 > n7:
    swp = n6
    n6 = n7
    n7 = swp
# fim do primeiro bloco

se n0 > n3:
    swp = n0
    n0 = n3
    n3 = swp
se n1 > n2:
    swp = n1
    n1 = n2
    n3 = swp
se n4 > n7:
    swp = n4
    n4 = n7
    n7 = swp
se n5 > n6:
    swp = n5
    n5 = n6
    n6 = swp
# fim do segundo bloco

se n0 > n1:
    swp = n0
    n0 = n1
    n1 = swp
se n2 > n3:
    swp = n2
    n2 = n3
    n3 = swp
se n4 > n5:
    swp = n4
    n4 = n5
    n5 = swp
se n6 > n7:
    swp = n6
    n6 = n7
    n7 = swp
# fim do terceiro bloco

se n0 > n7:
    swp = n0
    n0 = n7
    n7 = swp
se n1 > n6:
    swp = n1
    n1 = n6
    n6 = swp
se n2 > n5:
    swp = n2
    n2 = n5
    n5 = swp
se n3 > n4:
    swp = n3
    n3 = n4
    n4 = swp
# fim do quarto bloco

se n0 > n2:
    swp = n0
    n0 = n2
    n2 = swp
se n1 > n3:
    swp = n1
    n1 = n3
    n3 = swp
se n4 > n6:
    swp = n4
    n4 = n6
    n6 = swp
se n5 > n7:
    swp = n5
    n5 = n7
    n7 = swp
# fim do quinto bloco

se n0 > n1:
    swp = n0
    n0 = n1
    n1 = swp
se n2 > n3:
    swp = n2
    n2 = n3
    n3 = swp
se n4 > n5:
    swp = n4
    n4 = n5
    n5 = swp
se n6 > n7:
    swp = n6
    n6 = n7
    n7 = swp

At the end of the run, I guarantee that:

n0 <= n1 <= n2 <= n3 <= n4 <= n5 <= n6 <= n7

This particular sort network I used from the most optimized bitonic-merge-sort possible for parallelism use. Maybe it's the case for you to look for another sort network, such as network for bubble-sort or network for selection-sort.

    
26.09.2017 / 06:52