Skip to content

Instantly share code, notes, and snippets.

@basp1
Last active July 3, 2019 07:06
Show Gist options
  • Save basp1/5dfabdb57f0d3116189a3de21b5e7e61 to your computer and use it in GitHub Desktop.
Save basp1/5dfabdb57f0d3116189a3de21b5e7e61 to your computer and use it in GitHub Desktop.
binary search in J
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