Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
1  
$[x]$ ($x$ in square brackets) means the integer part of $x$; for ex., $[3.2]=3$. –  user35603 Apr 18 at 19:19

1 Answer 1

up vote 1 down vote accepted

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.