langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
51
Task/Binary-search/Objective-C/binary-search-1.m
Normal file
51
Task/Binary-search/Objective-C/binary-search-1.m
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (BinarySearch)
|
||||
// Requires all elements of this array to implement a -compare: method which
|
||||
// returns a NSComparisonResult for comparison.
|
||||
// Returns NSNotFound when not found
|
||||
- (NSInteger) binarySearch:(id)key;
|
||||
@end
|
||||
|
||||
@implementation NSArray (BinarySearch)
|
||||
- (NSInteger) binarySearch:(id)key {
|
||||
NSInteger lo = 0;
|
||||
NSInteger hi = [self count] - 1;
|
||||
while (lo <= hi) {
|
||||
NSInteger mid = lo + (hi - lo) / 2;
|
||||
id midVal = [self objectAtIndex:mid];
|
||||
switch ([midVal compare:key]) {
|
||||
case NSOrderedAscending:
|
||||
lo = mid + 1;
|
||||
break;
|
||||
case NSOrderedDescending:
|
||||
hi = mid - 1;
|
||||
break;
|
||||
case NSOrderedSame:
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return NSNotFound;
|
||||
}
|
||||
@end
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
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
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
49
Task/Binary-search/Objective-C/binary-search-2.m
Normal file
49
Task/Binary-search/Objective-C/binary-search-2.m
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (BinarySearchRecursive)
|
||||
// Requires all elements of this array to implement a -compare: method which
|
||||
// returns a NSComparisonResult for comparison.
|
||||
// Returns NSNotFound when not found
|
||||
- (NSInteger) binarySearch:(id)key inRange:(NSRange)range;
|
||||
@end
|
||||
|
||||
@implementation NSArray (BinarySearchRecursive)
|
||||
- (NSInteger) binarySearch:(id)key inRange:(NSRange)range {
|
||||
if (range.length == 0)
|
||||
return NSNotFound;
|
||||
NSInteger mid = range.location + range.length / 2;
|
||||
id midVal = [self objectAtIndex:mid];
|
||||
switch ([midVal compare:key]) {
|
||||
case NSOrderedAscending:
|
||||
return [self binarySearch:key
|
||||
inRange:NSMakeRange(mid + 1, NSMaxRange(range) - (mid + 1))];
|
||||
case NSOrderedDescending:
|
||||
return [self binarySearch:key
|
||||
inRange:NSMakeRange(range.location, mid - range.location)];
|
||||
default:
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
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
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
25
Task/Binary-search/Objective-C/binary-search-3.m
Normal file
25
Task/Binary-search/Objective-C/binary-search-3.m
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
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
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
30
Task/Binary-search/Objective-C/binary-search-4.m
Normal file
30
Task/Binary-search/Objective-C/binary-search-4.m
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
CFComparisonResult myComparator(const void *x, const void *y, void *context) {
|
||||
return [(id)x compare:(id)y];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
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
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue