{"size": 2158, "ext": "rktl", "max_stars_repo_path": "pkgs/racket-benchmarks/tests/racket/benchmarks/shootout/typed/heapsort.rktl", "max_stars_repo_name": "lkh01/racket", "max_stars_repo_stars_event_min_datetime": "2018-07-05T21:18:24.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-04T14:48:37.000Z", "max_issues_repo_path": "pkgs/racket-benchmarks/tests/racket/benchmarks/shootout/typed/heapsort.rktl", "max_issues_repo_name": "lkh01/racket", "max_issues_repo_issues_event_min_datetime": "2017-05-10T07:33:20.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-19T15:49:46.000Z", "max_forks_repo_path": "pkgs/racket-benchmarks/tests/racket/benchmarks/shootout/typed/heapsort.rktl", "max_forks_repo_name": "lkh01/racket", "max_forks_repo_forks_event_min_datetime": "2016-04-25T11:47:57.000Z", "max_forks_repo_forks_event_max_datetime": "2017-05-09T14:52:48.000Z"}
{"max_stars_count": 4, "max_issues_count": 9, "max_forks_count": 3, "avg_line_length": 30.8285714286, "max_line_length": 95, "alphanum_fraction": 0.4550509731}
;;; heapsort.scm

;; Prints 0.9990640717878372 instead of 0.9990640718 when n=1000.
;; Updated by Justin Smith
;;
;; Updated by Brent Fulgham to provide proper output formatting

(define IM   139968)
(define IA     3877)
(define IC    29573)

(: LAST Integer)
(define LAST 42)
(: gen_random (Float -> Float))
(define (gen_random max)
  (set! LAST (modulo (+ (* LAST IA) IC) IM))
  (/ (* max (exact->inexact LAST)) (exact->inexact IM)))

(: heapsort (Integer (Vectorof Float) -> (U Void True)))
(define (heapsort n ra)
  (let: ((ir : Integer n)
         (l : Integer (+ (quotient n 2) 1))
         (i : Integer 0)
         (j : Integer 0)
         (rra : Float 0.0))
    (let/ec: return : True
      (do: : Void
           ((bar : True #t))
           ((= 1 0))
        (cond ((> l 1)
               (set! l (- l 1))
               (set! rra (vector-ref ra l)))
              (else
               (set! rra (vector-ref ra ir))
               (vector-set! ra ir (vector-ref ra 1))
               (set! ir (- ir 1))
               (cond ((<= ir 1)
                      (vector-set! ra 1 rra)
                      (return #t)))))
        (set! i l)
        (set! j (* l 2))
        (do ((foo #t))
            ((> j ir))
          (cond ((and (< j ir) (< (vector-ref ra j) (vector-ref ra (+ j 1))))
                 (set! j (+ j 1))))
          (cond ((< rra (vector-ref ra j))
                 (vector-set! ra i (vector-ref ra j))
                 (set! i j)
                 (set! j (+ j i)))
                (else
                 (set! j (+ ir 1)))))
        (vector-set! ra i rra)))))

(: main ((Vectorof String) -> Void))
(define (main args)
  (let*: ((n : Integer
             (or (and (= (vector-length args) 1)
                      (assert (string->number (vector-ref args 0)) exact-nonnegative-integer?))
                 1))
          (last : Integer (+ n 1))
          (ary : (Vectorof Float) (make-vector last 0.0)))
    (do ((i 1 (+ i 1)))
        ((= i last))
      (vector-set! ary i (gen_random 1.0)))
    (heapsort n ary)
    (printf "~a\n"
            (real->decimal-string (vector-ref ary n) 10))))

(main (current-command-line-arguments))
