diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 04ec1f0fd..9200579b8 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -9,3 +9,4 @@ Maxime Dherbécourt Jess 3Jane Pen Pal Chinmaya Mahesh +Unlambder \ No newline at end of file diff --git a/book.json b/book.json index 3b3df4a48..06665f74c 100644 --- a/book.json +++ b/book.json @@ -82,6 +82,10 @@ { "lang": "go", "name": "Go" + }, + { + "lang": "racket", + "name": "Racket" } { "lang": "m", diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 4e3d6d829..dd2455e61 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -32,6 +32,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with [import:3-18, lang:"d"](code/d/bubble_sort.d) {% sample lang="go" %} [import:7-21, lang:"go"](code/go/bubbleSort.go) +{% sample lang="racket" %} +[import:5-19, lang:"racket"](code/racket/bubbleSort.rkt) {% endmethod %} ... And that's it for the simplest bubble sort method. diff --git a/chapters/sorting_searching/bubble/code/racket/bubbleSort.rkt b/chapters/sorting_searching/bubble/code/racket/bubbleSort.rkt new file mode 100644 index 000000000..458fdf931 --- /dev/null +++ b/chapters/sorting_searching/bubble/code/racket/bubbleSort.rkt @@ -0,0 +1,21 @@ +#lang racket + +(provide bubbleSort) + + +(define bubbleSort + (case-lambda [(l) (bubbleSort l (length l))] + [(l n) (if (= n 1) + l + (bubbleSort (pass l 0 n) (- n 1)))])) + +; a single pass, if this is the nth pass, then we know that the (n - 1) last elements are already sorted +(define (pass l counter n) + (let ([x (first l)] + [y (second l)] + [r (drop l 2)]) + (cond [(= (- n counter) 2) (cons (min x y) (cons (max x y) r))] + [(cons (min x y) (pass (cons (max x y) r) (+ counter 1) n))]))) + + +((lambda (x) (display (bubbleSort x))) (read))