Wednesday, April 19, 2017

fraction as index number?



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



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


No comments:

Post a Comment

analysis - Injection, making bijection

I have injection $f \colon A \rightarrow B$ and I want to get bijection. Can I just resting codomain to $f(A)$? I know that every function i...