March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,18 @@
>>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. binary-search.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 nums-area VALUE "01040612184356".
03 nums PIC 9(2)
OCCURS 7 TIMES
ASCENDING KEY nums
INDEXED BY nums-idx.
PROCEDURE DIVISION.
SEARCH ALL nums
WHEN nums (nums-idx) = 4
DISPLAY "Found 4 at index " nums-idx
END-SEARCH
.
END PROGRAM binary-search.

View file

@ -13,7 +13,7 @@
NSInteger hi = [self count] - 1;
while (lo <= hi) {
NSInteger mid = lo + (hi - lo) / 2;
id midVal = [self objectAtIndex:mid];
id midVal = self[mid];
switch ([midVal compare:key]) {
case NSOrderedAscending:
lo = mid + 1;
@ -31,21 +31,11 @@
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSArray *a = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 1],
[NSNumber numberWithInt: 3],
[NSNumber numberWithInt: 4],
[NSNumber numberWithInt: 5],
[NSNumber numberWithInt: 6],
[NSNumber numberWithInt: 7],
[NSNumber numberWithInt: 8],
[NSNumber numberWithInt: 9],
[NSNumber numberWithInt: 10],
nil];
NSLog(@"6 is at position %d", [a binarySearch:[NSNumber numberWithInt: 6]]); // prints 4
NSArray *a = @[@1, @3, @4, @5, @6, @7, @8, @9, @10];
NSLog(@"6 is at position %d", [a binarySearch:@6]); // prints 4
[pool drain];
}
return 0;
}

View file

@ -12,7 +12,7 @@
if (range.length == 0)
return NSNotFound;
NSInteger mid = range.location + range.length / 2;
id midVal = [self objectAtIndex:mid];
id midVal = self[mid];
switch ([midVal compare:key]) {
case NSOrderedAscending:
return [self binarySearch:key
@ -28,22 +28,11 @@
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSArray *a = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 1],
[NSNumber numberWithInt: 3],
[NSNumber numberWithInt: 4],
[NSNumber numberWithInt: 5],
[NSNumber numberWithInt: 6],
[NSNumber numberWithInt: 7],
[NSNumber numberWithInt: 8],
[NSNumber numberWithInt: 9],
[NSNumber numberWithInt: 10],
nil];
NSLog(@"6 is at position %d", [a binarySearch:[NSNumber numberWithInt: 6]
inRange:NSMakeRange(0, [a count])]); // prints 4
NSArray *a = @[@1, @3, @4, @5, @6, @7, @8, @9, @10];
NSLog(@"6 is at position %d", [a binarySearch:@6]); // prints 4
[pool drain];
}
return 0;
}

View file

@ -2,24 +2,14 @@
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSArray *a = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 1],
[NSNumber numberWithInt: 3],
[NSNumber numberWithInt: 4],
[NSNumber numberWithInt: 5],
[NSNumber numberWithInt: 6],
[NSNumber numberWithInt: 7],
[NSNumber numberWithInt: 8],
[NSNumber numberWithInt: 9],
[NSNumber numberWithInt: 10],
nil];
NSLog(@"6 is at position %lu", [a indexOfObject:[NSNumber numberWithInt: 6]
inSortedRange:NSMakeRange(0, [a count])
options:0
usingComparator:^(id x, id y){ return [x compare: y]; }]); // prints 4
NSArray *a = @[@1, @3, @4, @5, @6, @7, @8, @9, @10];
NSLog(@"6 is at position %lu", [a indexOfObject:@6
inSortedRange:NSMakeRange(0, [a count])
options:0
usingComparator:^(id x, id y){ return [x compare: y]; }]); // prints 4
[pool drain];
}
return 0;
}

View file

@ -1,30 +1,19 @@
#import <Foundation/Foundation.h>
CFComparisonResult myComparator(const void *x, const void *y, void *context) {
return [(id)x compare:(id)y];
return [(__bridge id)x compare:(__bridge id)y];
}
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int main(int argc, const char *argv[]) {
@autoreleasepool {
NSArray *a = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 1],
[NSNumber numberWithInt: 3],
[NSNumber numberWithInt: 4],
[NSNumber numberWithInt: 5],
[NSNumber numberWithInt: 6],
[NSNumber numberWithInt: 7],
[NSNumber numberWithInt: 8],
[NSNumber numberWithInt: 9],
[NSNumber numberWithInt: 10],
nil];
NSLog(@"6 is at position %d", CFArrayBSearchValues((CFArrayRef)a,
CFRangeMake(0, [a count]),
[NSNumber numberWithInt: 6],
myComparator,
NULL)); // prints 4
NSArray *a = @[@1, @3, @4, @5, @6, @7, @8, @9, @10];
NSLog(@"6 is at position %ld", CFArrayBSearchValues((__bridge CFArrayRef)a,
CFRangeMake(0, [a count]),
(__bridge const void *)@6,
myComparator,
NULL)); // prints 4
[pool drain];
}
return 0;
}

View file

@ -1,3 +1,3 @@
sub search (@a, Num $x --> Int) {
binary_search { $x <=> @a[$^i] }, 0, @a.end
sub search (@a, $x --> Int) {
binary_search { $x cmp @a[$^i] }, 0, @a.end
}

View file

@ -0,0 +1,4 @@
haystack = [0,1,4,5,6,7,8,9,12,26,45,67,78,90,98,123,211,234,456,769,865,2345,3215,14345,24324]
needles = [0,42,45,24324,99999]
needles.select{|needle| haystack.bsearch{|hay| needle <=> hay} } # => [0, 45, 24324]

View file

@ -0,0 +1,17 @@
fn binary_search(haystack: ~[int], needle: int) -> int {
let mut low = 0;
let mut high = haystack.len() as int - 1;
if high == 0 { return -1 }
while low <= high {
// avoid overflow
let mid = low + (high - low) / 2;
if haystack[mid] > needle { high = mid - 1 }
else if haystack[mid] < needle { low = mid + 1 }
else { return mid }
}
return -1;
}

View file

@ -0,0 +1,27 @@
PROGRAM:BINSEARC
:Disp "INPUT A LIST:"
:Input L1
:SortA(L1)
:Disp "INPUT A NUMBER:"
:Input A
:1→L
:dim(L1)→H
:int(L+(H-L)/2)→M
:While L<H and L1(M)≠A
:If A>M
:Then
:M+1→L
:Else
:M-1→H
:End
:int(L+(H-L)/2)→M
:End
:If L1(M)=A
:Then
:Disp A
:Disp "IS AT POSITION"
:Disp M
:Else
:Disp A
:Disp "IS NOT IN"
:Disp L1