« 日記 |Main| log »

« QuickSort祭り(一人) | Scheme/Lisp | Schemeで双方向リスト »

Schemeで破壊的クイックソート

一応できました。後は非破壊的クイックソートとのパフォーマンスの差を調べてみたいところです。

(define (_move_shift_one head tail)
  (if (eq? (cdr head) tail)
      (begin
        (set-car! (cdr head) (car head)) ; shift one
        head)
      (_move_shift_one (cdr head) tail)))

(define (_move_shift head tail)
  (if (eq? head tail)
      head
      (_move_shift head (_move_shift_one head tail))))
  
(define (moveBack src dst)
  (let ((tmp (car src)))
    (_move_shift dst src)
    (set-car! dst tmp)
    dst))

(define (sort! x)
  (partial_sort! x ())
  x)

(define (partial_sort! head tail)
  (let ((pivot (split head (cdr head) tail)))
    (if (eq? head pivot)
        ()
        (partial_sort! head pivot))
    (if (eq? (cdr pivot) tail)
        ()
        (partial_sort! (cdr pivot) tail))))
  
(define (split pivot cur tail)
  (if (eq? cur tail)
      pivot
      (if (< (car cur) (car pivot))
          (begin
            (moveBack cur pivot)
            (split (cdr pivot) (cdr cur) tail))
          (split pivot (cdr cur) tail))))

(define a '(3 2 4 1 5))
(sort! a) ;-> (1 2 3 4 5)

トラックバック(Trackback)

Trackback URL: http://www.nishiohirokazu.org/mt/mt-tb.cgi/166

ご意見・ご感想をお送りください(フィードバック)

(フィードバックはメールで送信され、基本的に表示されませんが、内容によっては公開させていただくこともございます。ご了承ください。Your comment doesn't appear the page immediately. If the comment has value to other people, it will be put on the page or subsequent entries. Thank you.)

上の情報は、いずれも未記入でかまいません。 All of above questions are optional.