Sha256: d988bfee68c91a157ba3fa67bd2a4f6b52acbf712f7a0468fd42f5c09ce87b2f

Contents?: true

Size: 1.5 KB

Versions: 268

Compression:

Stored size: 1.5 KB

Contents

#import "BinarySearchExample.h"

@implementation BinarySearch

- (instancetype)initWithArray:(NSArray<NSNumber *> *)array {
    self = [super init];
        
    if (self) {
        // This binary search algorithm only works if the array is already is ascending order.
        if ([array isEqualToArray:[array sortedArrayUsingSelector:@selector(compare:)]]) {
            _list = array;
        } else {
            return nil;
        }
    }
    
    return self;
}

- (NSInteger)searchFor:(int)number {
    NSInteger middleIndex = [self middle];
    int middle = [self.list[middleIndex] intValue];
    
    if (middle == number) {
        return middleIndex;
    } else if (middle > number) {
        NSArray *sublist = [self.list subarrayWithRange:NSMakeRange(0, middleIndex)];
        
        BinarySearch *binary = [[BinarySearch alloc] initWithArray:sublist];
        if (!binary) {
            return NSNotFound;
        }
        
        return [binary searchFor:number];
    } else { //!OCLint
        NSUInteger length = self.list.count - middleIndex;
        NSArray *sublist = [self.list subarrayWithRange:NSMakeRange(middleIndex, length)];
        if ([sublist isEqualToArray:self.list]) {
            return NSNotFound;
        }
        
        BinarySearch *binary = [[BinarySearch alloc] initWithArray:sublist];
        if (!binary) {
            return NSNotFound;
        }
        
        return [binary searchFor:number] + middleIndex;
    }
}

- (NSInteger)middle {
    return self.list.count / 2;
}

@end

Version data entries

268 entries across 268 versions & 1 rubygems

Version Path
trackler-2.1.0.0 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.55 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.54 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.53 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.52 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.51 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.50 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.49 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.48 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.47 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.46 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.45 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.44 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.43 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.42 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.41 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.40 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.39 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.38 tracks/objective-c/exercises/binary-search/BinarySearchExample.m
trackler-2.0.8.37 tracks/objective-c/exercises/binary-search/BinarySearchExample.m