Created
July 11, 2024 06:01
-
-
Save narskidan/036641511f9afd2be9c755180a806de7 to your computer and use it in GitHub Desktop.
Merge sort in Hoon
This file contains 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
|% | |
++ merge | |
|= [a=(list @) b=(list @)] | |
^- (list @) | |
=| c=(list @) | |
|- | |
=/ empty-a =(0 (lent a)) | |
=/ empty-b =(0 (lent b)) | |
?: ?|(empty-a empty-b) | |
:(weld c a b) | |
=/ first-a (snag 0 a) | |
=/ first-b (snag 0 b) | |
?: (lth first-a first-b) | |
$(c (snoc c first-a), a (slag 1 a)) | |
$(c (snoc c first-b), b (slag 1 b)) | |
++ sort | |
|= l=(list @) | |
?: =(1 (lent l)) | |
l | |
=/ middle (div (lent l) 2) | |
=/ left $(l (scag middle l)) | |
=/ right $(l (slag middle l)) | |
(merge left right) | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment