
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort




Task

Sort an integer array with the   radix sort algorithm.

The primary purpose is to complete the characterization of sort algorithms task.




;;; An illustrative implementation of the radix-10 example at
;;; https://en.wikipedia.org/w/index.php?title=Radix_sort&oldid=1070890278#Least_significant_digit
 
(cond-expand
  (r7rs)
  (chicken (import (r7rs))))
 
(import (scheme base))
(import (scheme write))
 
(define (sort-by-decimal-digit data power-of-10)
  (define bins (make-vector 10 '()))
  (do ((i (- (vector-length data) 1) (- i 1)))
      ((= i -1))
    (let* ((element (vector-ref data i))
           (digit (truncate-remainder
                   (truncate-quotient element power-of-10)
                   10)))
      (vector-set! bins digit
                   (cons element (vector-ref bins digit)))))
  (let ((non-zero-found
         (let loop ((i 1))
           (cond ((= i (vector-length bins)) #f)
                 ((pair? (vector-ref bins i)) #t)
                 (else (loop (+ i 1)))))))
    (when non-zero-found
      (let ((i 0))
        (do ((j 0 (+ j 1)))
            ((= j (vector-length bins)))
          (do ((p (vector-ref bins j) (cdr p)))
              ((null? p))
            (vector-set! data i (car p))
            (set! i (+ i 1))))))
    (not non-zero-found)))
 
(define (radix-sort data)
  (let loop ((power-of-10 1))
    (let ((done (sort-by-decimal-digit data power-of-10)))
      (unless done
        (loop (* 10 power-of-10))))))
 
(define data (vector-copy #(170 45 75 90 2 802 2 66)))
(write data)
(newline)
(radix-sort data)
(write data)
(newline)