#lang racket/gui
(require plot
(planet williams/animated-canvas/animated-canvas))
(define (plot-vector v canvas)
(define dc (send canvas get-dc))
(define width (send canvas get-width))
(define height (send canvas get-height))
(parameterize ((plot-decorations? #f))
(plot/dc (discrete-histogram
(for/list ((item (in-vector v))
(i (in-naturals)))
(vector i item)))
dc 0 0 width height))
(send canvas swap-bitmaps)
(yield))
(define (animated-insertion-sort v)
(plot-vector v canvas)
(for ((j (in-range 1 (vector-length v))))
(define key (vector-ref v j))
(define i (- j 1))
(let loop ()
(when (and (>= i 0)
(> (vector-ref v i) key))
(vector-set! v (+ i 1) (vector-ref v i))
(plot-vector v canvas)
(set! i (- i 1))
(loop)))
(vector-set! v (+ i 1) key)
(plot-vector v canvas)))
(define (main)
(define array
(build-vector
50
(lambda (i)
(random 100))))
(animated-insertion-sort array))
(define frame
(instantiate frame% ("Insertion Sort Animation")))
(define canvas
(instantiate animated-canvas%
(frame)
(style '(border))
(min-width 600)
(min-height 300)))
(send frame show #t)
(main)