fraction as index number?

115 Views Asked by At

given these inputs x = 4, S = [1 2 3 4 5 6 7 8 9 10], and n = 10

 search (x,S,n) {
     i = 1
     j = n
     while i < j {
         m = [(i+j)/2]
         if x > Sm then i=n+1
         else j = m
     end
     if x = Si then location = i
     else location = 0

This algorithm is from my discrete math hw. I'm confused as to what Sm would equal on the first iteration because m would be $\frac{11}{2}$. If i use a fraction as the index do I round down? Is there a general rule for this? Am I making any sense? Help pls

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, you should round down (or in other words, take only the integer part).

$$[11/2]=[5.5]=5$$

This is known as the floor function in mathematics (usually there is a floor(x) function in programming languages).