Last active
July 3, 2019 07:06
-
-
Save basp1/5dfabdb57f0d3116189a3de21b5e7e61 to your computer and use it in GitHub Desktop.
binary search in J
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
mid =: ([: <. [: -: #) : [: | |
take_mid =: ({~ mid) : [: | |
compare_mid =: [: : (> take_mid) | |
take_left =: ({.~ mid) : [: | |
take_right =: (}.~ mid) : [: | |
cut_compared =: (take_left@])`(take_right@]) @. compare_mid | |
binary_search =: ([ $: cut_compared)`(take_mid@]) @. (= take_mid) :: _ | |
NB. (binary_search&(i.5))"0 (i.15) - 5 | |
NB. _ _ _ _ _ 0 1 2 3 4 _ _ _ _ _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment