lua: table.sort insertion-sort → quicksort; 30k sort.lua still timeouts (interpreter-bound)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 21:02:33 +00:00
parent 57516ce18e
commit 12b02d5691
4 changed files with 61 additions and 44 deletions

View File

@@ -1132,23 +1132,39 @@
((= comp nil) (lua-lt a b))
(else (lua-truthy? (lua-call comp a b))))))
(define
insert-sorted
(fn (i)
(when (> i 1)
(let ((v (get t (str i))) (prev (get t (str (- i 1)))))
(when (lt? v prev)
(begin
(dict-set! t (str i) prev)
(dict-set! t (str (- i 1)) v)
(insert-sorted (- i 1))))))))
(define
outer
(fn (i)
(when (<= i n)
ts-swap
(fn (i j)
(let ((vi (get t (str i))) (vj (get t (str j))))
(begin
(insert-sorted i)
(outer (+ i 1))))))
(outer 2)
(dict-set! t (str i) vj)
(dict-set! t (str j) vi)))))
(define
ts-partition
(fn (lo hi)
(let ((pivot (get t (str hi))) (i (- lo 1)))
(begin
(define
pt-loop
(fn (j)
(when (< j hi)
(begin
(when (lt? (get t (str j)) pivot)
(begin
(set! i (+ i 1))
(ts-swap i j)))
(pt-loop (+ j 1))))))
(pt-loop lo)
(ts-swap (+ i 1) hi)
(+ i 1)))))
(define
ts-qsort
(fn (lo hi)
(when (< lo hi)
(let ((p (ts-partition lo hi)))
(begin
(ts-qsort lo (- p 1))
(ts-qsort (+ p 1) hi))))))
(ts-qsort 1 n)
nil)))))
(define