What happens to variable i in this algorithm? Sequence inversion algorithm

Asked

Viewed 238 times

2

Good evening. I started to study data structure in support of the book Data Structure and Algorithms 2nd ed (SZWARCFITER/MARKENZON). The first algorithm of the book deals with reversing the elements of the sequence in the vector, exchanging the position of the last element with the first, successively until the last element. The algorithm is this:

algoritmo 1.1: Inversão de uma sequencia
   para i <- 1...[n/2] faça
       temp <- S[i]
       S[i] <- S[n - i + 1]
       S[n -i + 1] <- temp

I wrote the algorithm on Portugol and added an excerpt that completes the list.

var


S:VETOR[1..10] de INTEIRO
  i,j,temp,t: INTEIRO
inicio

para i de 1 ate 10 faca
     S[i] <- i
fimpara

para i de 1 ate 10 faca
     escreva(S[i], " ")
fimpara

 Escreval(" ")

 para j de 1 ate 10 faca
      temp <- S[j]
      S[j] <- S[10-j+1]
      t <- S[10-j+1]
      se (j>5) entao
              S[j] <- -j+i
              t <- -j+i
      fimse
      escreva(t, " ")
 fimpara

fimalgoritmo

My question is about the variable i. Why does it assume the value 11 after printing the list? Before arriving at this code, I used an auxiliary variable k, to list the numbers smaller than 6 in reverse order, but I realized that i assumed the value 11, so I started doing -j+i. Somebody explain this to me?

  • From what little I know of algorithms, your vector should not start at "0" ?

  • Yes, it starts from 0. S[1] = zero position of vector S.

1 answer

0


Look at what I know of algorithm, portugol and everything else, if you start at 0 or 1 or 2 whatever, what matters is that you know what you’re doing, going on, what you can do is the following: at the time you will assign the values of i in another vector, while you assign the inverse already assigns again, it mounted on the main vector.

I’ll try to explain with an example:

funcao inversao():inteiro
var
inversor :vetor [0..9] de inteiro // vetor inversor
contador_inversor:inteiro
inicio
escreval (" A ORDEM INVERSA É: ")
contador <- 10
para contador_inversor de 0 ate 12 faca
     inversor[contador_inversor] <- c[contador]
     contador <- contador - 1
fimpara
para contador de 0 ate 12 faca
     c[contador] <- inversor[contador]
     escreva(" ", c[contador])
fimpara
escreval(" ")
fimfuncao

I give you this example to take as a basis, because it is of a project that I did, if it did not serve me tell me that I help you better, but try to interpret from this that works for me here.

  • I get it. What you did is an alternative to the problem. My question is about the behavior of variable i and why it assumes the value 11, since the number of iterations is 10.

  • because to each interaction he adds 1, in case sum 1 to variable i, say that it starts in 1, makes the loop, adds 1 more would i <- i + 1 but the loop to even do already does that, of adding, i + 1, and dai deopis to add, that 1 ai in the variable and assign to i, he compares with the value ATE in case 10 if it is not 10 then he executes the loop and adds one more until arriving at 10 arriving at 10 he executes the loop again, and adds + 1 being 11 and then he falls out of the loop as the loop only asked for up to 10 and finishing! Is that it? I was clear? Or was there something I didn’t understand?

  • That’s what I wanted to know. I had a slight intuition, but not the certainty that this could be from the Visualg program, not the algorithm. Thank you.

Browser other questions tagged

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