Binary search: Difference between revisions
→{{header|jq}}: gojq; bsearch
No edit summary |
(→{{header|jq}}: gojq; bsearch) |
||
Line 4,542:
]</pre>
=={{header|jq}}==
{{works with|jq}}
If the input array is sorted, then binarySearch(value) as defined here will return an index (i.e. offset) of value in the array if the array contains the value, and otherwise (-1 - ix), where ix is the insertion point, if the value cannot be found. binarySearch will always terminate.▼
'''Also works with gojq, the Go implementation of jq'''
Recursive solution:<syntaxhighlight lang="jq">def binarySearch(value):▼
jq and gojq both have a binary-search builtin named `bsearch`.
In the following, a parameterized filter for performing a binary search of a sorted JSON array is defined.
▲
binarySearch will always terminate. The inner function is recursive.
# To avoid copying the array, simply pass in the current low and high offsets
def binarySearch(low; high):
Line 4,558 ⟶ 4,566:
{{Out}}
2
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">/**
|