Contents    Page-10    Prev    Next    Page+10    Index   

Binary Array Search Example


(defun searcharr (arr low high goal)
  (let (root)
    (setq root (truncate (+ low high) 2))
    (if (< high low)
        -1               ; arr[root]
        (if (string= (aref arr root) goal)
            root
            (if (string< goal (aref arr root))
                (searcharr arr low
                           (- root 1) goal)
                (searcharr arr (+ root 1)
                           high goal) ) ) ) ))


>(setq myarr '#("ape" "bat" "bee" "cat" "dog" ...))

>(searcharr myarr 0 6 "bee")
  1> (SEARCHARR #("ape" "bat" "bee" "cat" ...) 0 6
                "bee")
    2> (SEARCHARR #("ape" "bat" "bee" ...) 0 2 "bee")
      3> (SEARCHARR #("ape" "bat" "bee" ...) 2 2
                    "bee")
      <3 (SEARCHARR 2)
    <2 (SEARCHARR 2)
  <1 (SEARCHARR 2)
2