HOME

The Racket Examples

Introduction

Add something here ...

Build Notes

Add something here ...

Parallel sieve of Erathostenes

Add something here ...


  

The 'HEAPSORT' Algorithm

Add something here ...

#lang racket
(require (only-in srfi/43 vector-swap!))

(define (heap-sort! xs)
  (define (ref i) (vector-ref xs i))
  (define (swap! i j) (vector-swap! xs i j))
  (define size (vector-length xs))
  
  (define (sift-down! r end)
    (define c (+ (* 2 r) 1))
    (define c+1 (+ c 1))
    (when (<= c end)
      (define child 
        (if (and (<= c+1 end) (< (ref c) (ref c+1)))
            c+1 c))
      (when (< (ref r) (ref child))
        (swap! r child))
      (sift-down! child end)))
  
  (for ([i (in-range (quotient (- size 2) 2) -1 -1)])
    (sift-down! i (- size 1)))
  
  (for ([end (in-range (- size 1) 0 -1)])
    (swap! 0 end)
    (sift-down! 0 (- end 1)))
  xs)

The complete source file for a Heapsort in the Racket programmming language.


kc4zvw@www:~$ racket uriah.rkt
'done
'#(2 4 5 7 8 9 13 14 15 16 17 20 23 24 27 28 31 34 35 36 39 40 43 45 47 48 49 51 55 56 57 59 61 62 65 68 70 71 72 73 74 76 77 78 79 82 85 87 88 89)
kc4zvw@www:~$

  

Summary

Add something here ...

Miscellaneous

Add something here ...





Revised: Thursday, April 04, 2024 at 01:00:43 AM (EDT)