Visualg - 5 larger issues

Asked

Viewed 1,116 times

0

I’m getting 5 values and need to sort from smaller to larger, without using loop or array.

I was only able to discover the smallest and the greatest; how could I discover the middle ones? Follow what I have 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
  • Define "from the middle".

  • 1

    @Maniero The third.

1 answer

2

Your writing is bad. Did not make clear what the expected result, nor what was your doubt.

I think you’re wondering how to sort the variables n1 until n5 to print them. Correct? Well, this is a great case for studying ordering networks.

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

An ordination network is composed of horizontal wires representing the positions in the array, and also by vertical bridges connecting wires. Every bridge connects only and exclusively two wires; on each bridge, the elements of the two connected wires are compared and, if necessary, exchanged. It also has another interesting property in sorting networks: time passes from left to right, and a wire can only be on a bridge at the same time.

rede de ordenação para 8 fios

In your case, each position is defined by a variable, not by a vector position.

You can search which ordering network is most optimized for five positions, I will go through here the pseudocode for 8 variables. Note also that I already consider that n0 to n7 already comes with the values filled from somewhere. Also note that use positional indexing style , so I 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 execution, I guarantee:

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

This specific ordering network I used the most optimized bitonic-merge-Sort possible for the use of parallelism. Maybe it’s the case that you look for another sort network, such as network for Bubble-Sort or network for Selection-Sort.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.