timsort-suite

Fri Aug 29 13:43:41Z 2025

Creative Commons LicenseCreative Commons LicenseCreative Commons License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

Table of contents
  1. test/simple: pass
  2. test/iota: pass
  3. test/iota/sort: pass
  4. test/timtros/iota: pass
  5. test/tros/iota: pass
  6. test/already-sorted: pass
  7. test/already-sorted/sort: pass
  8. test/already-sorted/primitive: pass
  9. test/primitive: pass

Tests summary

scheme code
((ran 9) (failed 0))

Here is a test suite for the timsort function, which implements the Timsort algorithm, a hybrid sorting algorithm derived from merge sort and insertion sort. It is designed to perform well on many kinds of real-world data. The implementation follows the specifications outlined in [1][2] that we extracted in [3] and we compare it against the built-in sort function [4][5]. For the sake of completeness, we report their implementation of the sort! function, which is a wrapper around the merge! function, which merges two sorted sequences. The sort! function sorts a sequence in place using a provided comparison function less?.

scheme code
(define (sort! seq less?)
  (define (step n)
    (cond ((> n 2)
           (let* ((j (quotient n 2)) (a (step j)) (k (- n j)) (b (step k)))
             (merge! a b less?)))
          ((= n 2)
           (let ((x (car seq)) (y (cadr seq)) (p seq))
             (set! seq (cddr seq))
             (if (less? y x) (begin (set-car! p y) (set-car! (cdr p) x)))
             (set-cdr! (cdr p) '())
             p))
          ((= n 1) (let ((p seq)) (set! seq (cdr seq)) (set-cdr! p '()) p))
          (else '())))
  (if (vector? seq)
    (let ((n (vector-length seq)) (vec seq))
      (set! seq (vector->list seq))
      (do ((p (step n) (cdr p)) (i 0 (+ i 1)))
          ((null? p) vec)
        (vector-set! vec i (car p))))
    (step (length seq))))
scheme code
(define (merge! a b less?)
  (define (loop r a b)
    (if (less? (car b) (car a))
      (begin
        (set-cdr! r b)
        (if (null? (cdr b)) (set-cdr! b a) (loop b a (cdr b))))
      (begin
        (set-cdr! r a)
        (if (null? (cdr a)) (set-cdr! a b) (loop a (cdr a) b)))))
  (cond ((null? a) b)
        ((null? b) a)
        ((less? (car b) (car a))
         (if (null? (cdr b)) (set-cdr! b a) (loop b a (cdr b)))
         b)
        (else (if (null? (cdr a)) (set-cdr! a b) (loop a (cdr a) b)) a)))

1. test/simple: pass

scheme code
(define (test/simple _)
  (⊦= '(1 2 3 4 5) (timsort '(5 4 3 2 1)))
  (⊦= '(1 2 3 4 5) (timsort '(1 2 3 4 5)))
  (⊦= '(1 2 3 4 5) (timsort '(3 2 1 5 4)))
  (⊦= '(1) (timsort '(1)))
  (⊦= '() (timsort '())))
scheme code
((eta 0.0) (memory #(50331648 24945128 1048576)) (stdout "") (stderr ""))

2. test/iota: pass

Here we test the timsort function with a large list of integers from 0 to 1000000 elements long. The list is sorted in descending order, and we expect the result to be a list sorted in ascending order.

scheme code
(define (test/iota _) (⊦= r (timsort (iota n (sub1 n) -1))))
scheme code
((eta 0.35) (memory #(100663296 48941800 1048576)) (stdout "") (stderr ""))

3. test/iota/sort: pass

scheme code
(define (test/iota/sort _) (⊦= r (sort (iota n (sub1 n) -1) <)))
scheme code
((eta 0.331) (memory #(201326592 48942440 1048576)) (stdout "") (stderr ""))

4. test/timtros/iota: pass

scheme code
(define (test/timtros/iota _) (⊦= (iota n (sub1 n) -1) (timtros r)))
scheme code
((eta 0.311) (memory #(201326592 72943144 1048576)) (stdout "") (stderr ""))

5. test/tros/iota: pass

scheme code
(define (test/tros/iota _) (⊦= (iota n (sub1 n) -1) (reverse (sort r <))))
scheme code
((eta 0.299) (memory #(201326592 72943824 1048576)) (stdout "") (stderr ""))

6. test/already-sorted: pass

scheme code
(define (test/already-sorted _) (⊦= r (timsort r)))
scheme code
((eta 0.411) (memory #(201326592 48944528 1048576)) (stdout "") (stderr ""))

7. test/already-sorted/sort: pass

scheme code
(define (test/already-sorted/sort _) (⊦= r (sort r <)))
scheme code
((eta 0.287) (memory #(201326592 48945160 1048576)) (stdout "") (stderr ""))

8. test/already-sorted/primitive: pass

scheme code
(define (test/already-sorted/primitive _) (⊦= r (timsort/primitive r)))
scheme code
((eta 0.123) (memory #(201326592 48945864 1048576)) (stdout "") (stderr ""))

9. test/primitive: pass

scheme code
(define (test/primitive _)
  (⊦= (sort '(5 4 3 2 1) <) (timsort/primitive '(5 4 3 2 1)))
  (⊦= '(hello world) (timsort/primitive '(world hello))))
scheme code
((eta 0.0) (memory #(201326592 48946496 1048576)) (stdout "") (stderr ""))

References
[1] CPython: listobject.c
[2] CPython: listsort.txt
[3] Github: timsort.c
[4] SRFI 95
[5] Wikipedia: Sorting algorithm