summaryrefslogtreecommitdiffstats
path: root/tests/010/sort.tl
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-05-01 16:42:10 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-05-01 16:42:10 -0700
commit558363cceda60c12b8fd22cda399e2e39dc11bac (patch)
tree75b1f4d714d2e0ef0d4c13cd4dba32a2fa91cb98 /tests/010/sort.tl
parentcb9b5f6e8ffbec96b5c22ab89bc6852f27dd29b8 (diff)
downloadtxr-558363cceda60c12b8fd22cda399e2e39dc11bac.tar.gz
txr-558363cceda60c12b8fd22cda399e2e39dc11bac.tar.bz2
txr-558363cceda60c12b8fd22cda399e2e39dc11bac.zip
sort: replace Lomuto partitioning with Hoare
I'm seeing numbers aobut the same performance on a sorted vector of integers, and 21% faster on vector of N random integers in the range [0, N). Also, this original algorithm handles well the case of an array consisting of a repeated value. The code we are replacing degrates to quadratic time. * lib.c (med_of_three, middle_pivot): We don't use the return value, so don't calculate and return one. (quicksort): Revise to Hoare: scanning from both ends of the array, exchanging elements. * tests/010/sort.tl: New file. We test sort with lists and vectors from length zero to eight, all permutations.
Diffstat (limited to 'tests/010/sort.tl')
-rw-r--r--tests/010/sort.tl15
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/010/sort.tl b/tests/010/sort.tl
new file mode 100644
index 00000000..40ba8519
--- /dev/null
+++ b/tests/010/sort.tl
@@ -0,0 +1,15 @@
+(load "../common")
+
+(test (sort ()) nil)
+
+(let* ((list (conses '(1 2 3 4 5 6 7 8)))
+ (sp (uniq [mapcar sort (perm list (len list))])))
+ (mvtest (len sp) 1
+ (car sp) list))
+
+(test (sort #()) #())
+
+(let* ((vec (conses #(1 2 3 4 5 6 7 8)))
+ (sp (uniq [mapcar sort (perm vec (len vec))])))
+ (mvtest (len sp) 1
+ (car sp) vec))