Sorting algorithms/Counting sort: Difference between revisions
Content deleted Content added
Racket version |
|||
Line 1,290:
(define (counting-sort xs min max)
(define ns (make-vector (+ max (- min) 1) 0))
(for ([x xs])
(vector-set! ns (- x min) (+ (vector-ref ns (- x min)) 1)))
(define ys (vector-copy xs))
(for/fold ([i 0]) ([n ns] [x (in-naturals
(for ([j (in-range i (+ i n ))])
(vector-set! ys j (+ x min)))
(+ i n))
ys)
(counting-sort #(0 9 3 8 1 -1 1 2 3 7 4)
</lang>
Output:
<lang racket>
'#(
</lang>
|