LoginSignup
2
2

More than 5 years have passed since last update.

全探索と二分探索

Last updated at Posted at 2012-04-29

全探索

search.scm
(define (binarysearch lst n)
  (let loop ((lst lst))
    (receive(left right)
      (split-at lst (div (length lst) 2))
        (cond ((null? left)#f)
              ((null? right)#f)
              ((eq? (last left) n)#t)
              ((eq? (car right) n)#t)
              ((> (last left) n)(loop left))
              ((< (car right) n)(loop right)))))))

二分探索

binarysearch.scm
(define (binarysearch lst n)
  (let loop ((lst lst))
    (receive(left right)
      (split-at lst (div (length lst) 2))
        (cond ((null? left)#f)
              ((null? right)#f)
              ((eq? (last left) n)#t)
              ((eq? (car right) n)#t)
              ((> (last left) n)(loop left))
              ((< (car right) n)(loop right))))))

結果

(1 3 5 11 12 13 17 22 25 28)から25を探索するとき

全探索

(1 3 5 11 12 13 17 22 25 28)
(3 5 11 12 13 17 22 25 28)
(5 11 12 13 17 22 25 28)
(11 12 13 17 22 25 28)
(12 13 17 22 25 28)
(13 17 22 25 28)
(17 22 25 28)
(22 25 28)
(25 28)

二分探索

(1 3 5 11 12):(13 17 22 25 28)
(13 17):(22 25 28)
(22):(25 28)

二分探索のほうが圧倒時に…はい

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2