RosettaCodeData/Task/Binary-search/FutureBasic/binary-search-1.basic

28 lines
616 B
Plaintext

include "NSLog.incl"
NSInteger local fn BinarySearch( array as CFArrayRef, key as CFTypeRef )
NSInteger lo = 0
NSInteger hi = len(array) - 1
while ( lo <= hi )
NSInteger i = lo + (hi - lo) / 2
CFTypeRef midVal = array[i]
select ( fn NumberCompare( midVal, key ) )
case NSOrderedAscending
lo = i + 1
case NSOrderedDescending
hi = i - 1
case NSOrderedSame:
return i
end select
wend
end fn = NSNotFound
void local fn DoIt
CFArrayRef a = @[@1, @3, @4, @5, @6, @7, @8, @9, @10]
NSLog(@"6 is at position %d", fn BinarySearch( a, @6 ) ) // prints 4
end fn
fn DoIt
HandleEvents