u3qb_sort use quicksort#851
Merged
Merged
Conversation
aad3320 to
142c070
Compare
Contributor
Author
|
dont merge it yet, I didnt check for stability |
Contributor
Author
|
+sort reverses the list if comparison gate always returns yes, and preserves the order if it always returns no. is this intentional? |
Contributor
Author
|
rewrote the jet to use merge sort for stability. Notice the reversed comparison here: Lines 38 to 48 in cccde8c This is done in order to mimick the behavior of +sort function in Hoon, where the first element of the array is taken as a pivot and then it is passed to the comparator gate as a second argument, the first being the compared list element. |
Contributor
Author
|
tested sort correctness with this dojo expression: =/ gat1 |=([[@ v1=@] @ v2=@] (lte v1 v2))
=/ gat2 |=([[@ v1=@] @ v2=@] (lth v1 v2))
!. ^- ~
=/ one-done |
=/ gat=$-([[@ @] @ @] ?) gat1
|- ^- ~
=* gat-loop $
=* vals-num 5
=* len-list 10
=/ perm (pow vals-num len-list)
|- ^- ~
=* perm-loop $
?: =(perm 0)
?: one-done ~
gat-loop(gat gat2, one-done &)
=/ l=(list (pair @ @)) :: idx and value
=/ c len-list
|- ^- (list (pair @ @))
=* list-loop $
?: =(c 0) ~
:- [(sub len-list c) (mod perm vals-num)]
list-loop(c (dec c), perm (div perm vals-num))
::
=/ l1 (sort l gat)
=/ l2 .*([-:sort [l gat] +>:sort] -:sort)
?. =(l1 l2)
~| ^- (list [@ @]) l1
~| ;; (list [@ @]) l2
!!
~? =(0 (mod perm 10.000)) perm
perm-loop(perm (dec perm))Changing comparison in sort jet to u3_noun sam = u3nc(u3k(arr_u[i_w]), u3k(arr_u[j_w]));
c3_o hoz_o = u3x_loob(u3j_gate_slam(sit_u, sam));
if ( c3y == hoz_o )
// ...would make the expression fail |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
+sortjet by building an array of nouns to run quicksort algorithm on. Has much better memory usage for large lists and appears to be faster than the previous implementation:~:(sort (gulf 1 100.000) gth)used to run out of memory, but not anymore, and~:(sort (gulf 1 10.000) gth)runs about 2x faster.