その他のソート
ソース
(use srfi-43)
(define (stooge-sort ls)
(let1 vect (list->vector ls)
(let rec ((head 0)(tail (- (vector-length vect) 1)))
(when (< (vector-ref vect tail)
(vector-ref vect head))
(vector-swap! vect head tail))
(when (<= 3 (+ (- tail head) 1))
(let1 index-of-1/3 (quotient (+ (- tail head) 1) 3)
(rec head (- tail index-of-1/3))
(rec (+ head index-of-1/3) tail)
(rec head (- tail index-of-1/3))))
vect)))
(use gauche.sequence)
(define (test sorter n)
(for-each (^i (let1 ls (shuffle (iota (expt 10 i)))
(print "; length = " (expt 10 i))
(time (sorter ls))
(print)))
(iota n 2)))
(test stooge-sort 2)
0 件のコメント:
コメントを投稿