All pastes #979031 Raw Edit

Pedro

public text v1 · immutable
·published 2008-04-10 01:06 UTC
rendered paste body
; Bubblesort implementation in Scheme
; Author: Pedro Matiello

; Move x'th element from vector V up in the vector until a bigger element is found
(define _bubble
    (lambda (V x)
      (when (< x (- (vector-length V) 1))
        (when (> (vector-ref V x) (vector-ref V (+ x 1)))
          (let ((tmp (vector-ref V x))) (vector-set! V x (vector-ref V (+ x 1))) (vector-set! V (+ x 1) tmp) )
          (_bubble V (+ x 1))
          )
        )
      V
      )
    )

; Call _bubble for each position in vector V, from the last to the first
(define _bubblesort
    (lambda (V x)
      (when (> x -1)
        (_bubble V x)
        (_bubblesort V (- x 1))
        )
      V
      )
    )

; Start bubblesort with the adequate function call to _bubblesort
(define bubblesort
    (lambda (V)
      (_bubblesort V (vector-length V))
      )
    )