According to my teacher the outer for loop repeats once in this algorithm . why?

43 Views Asked by At

This is the pseudocode for insertion sort. According to my teacher the for loop repeats once. Can someone explain why?

for i<-2,n do 
    aux<-v [i] 
    j<-i-1 
    while aux<v [j] AND j>=0 do 
        v[j+1] <-v [j] 
        j<-j-1 
    end while
    v[j+1] <-aux 
end for
1

There are 1 best solutions below

1
aliouche mohamed On

it does not repeat once , the for loop here has one and only one condition it's n>i (or n>=i not sure xd), and i isn't modified inside the for loop , so each repetition (iteration) is equivalent to 1 , this means the number of digits for i to reach n = the number of iterations so it's (n-i) time