Binary search/Talk

HomePage | Recent changes | View source | Discuss this page | Page history | Log in |

Printable version | Privacy policy

I changed the pseudocode since it was incorrect.dze27


Also, perhaps we need some sort of consistent pseudo-language for specifying pseudocode. Something like they do in Introduction to Algorithms, or smilar dze27


I would as well appreciate an implementation form in a real-world programming language -- HJH


Pseudocode:

function binary-search(L,V)
  set start = 1
  set end = N
  repeat while start <= end
    set middle = (start + end) div 2
    if V = L[middle]
      return success    
    else-if V < L[middle]
      set end = middle - 1 
    else-if (V > L[middle])
      set start = middle + 1
    end-if
  end-repeat
  return failure
end-function

Notes: 
 div is integer division (discard any remainder)