www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - binary search

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
We have:
     // sorted values
     size_t lines  = [20, 1755, 1756, 1757, 1798, 1824, 1825, 
1839, 1840];
     size_t search = 21;

Goal:
     // Fast find index of the '21' in ordered array 'lines'
     auto found = lines.binarySearch( 20 );     // 0 - index
     auto low   = lines.binarySearchLow( 21 );  // 0 - near lowest 
index

Where is the implementation of "binary search", please?
Dec 06 2020
parent reply drug <drug2004 bk.ru> writes:
Phobos provides this by SortedRange:
https://dlang.org/phobos/std_range.html#.SortedRange

Example of usage:
https://run.dlang.io/is/WW2bn0
Dec 06 2020
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Monday, 7 December 2020 at 06:24:27 UTC, drug wrote:
 Phobos provides this by SortedRange:
 https://dlang.org/phobos/std_range.html#.SortedRange

 Example of usage:
 https://run.dlang.io/is/WW2bn0
Thanks! :-)
Dec 06 2020