Ads

Wednesday, 23 October 2019

Algo - Linear Searching



Linear search is search each element one after the other. 

Here is the example, written in Objective for 1 - dimension array linear search algorithm.


#import

@interface AlgorithmSearch : NSObject
-(int)searchLinear: (NSArray *) arr searchingElement: (int) ele;
@end

@implementation AlgorithmSearch

-(id)init {
    self = [super init];
    return self;
}

// Linear Search Algoithm
-(int)searchLinear: (NSArray *) arr searchingElement: (int) ele {
    
    // We have take the algorithm and loop all the elements.
    // For linear search the complexity is O(n*m). Here m = 1(1 dimention array) and n = number of elements in the array.
    
    for (int obj = 0; obj < arr.count; obj++) {
        int value = [arr[obj] intValue];
        if (ele == value ) {
            return obj;
        }
    }
    return -1;
}
@end


int main() {
    @autoreleasepool {
        NSArray *elementsArray = @[@30,@40,@80,@70];
        int searchingElement = 80;
        
        AlgorithmSearch * algoSearch = [[AlgorithmSearch alloc] init];
        int index = [algoSearch searchLinear:elementsArray searchingElement:searchingElement];
        NSLog(@"%d",index);
        
        
    }
}



The output will be '2' because we are searching the element 80 which is at the index of 2 and we can say that the position is '3'.

Now let's see how we do it for 2-D array, so we can write for M-D by using the same logic.




@interface AlgorithmSearch : NSObject
-(NSDictionary *)multiDimensionLinearSearch: (NSArray *) arr searchingElement: (int) ele;
@end

@implementation AlgorithmSearch

-(id)init {
    self = [super init];
    return self;
}

// M-D Linear Search Algoithm
-(NSDictionary *)multiDimensionLinearSearch: (NSArray *) arr searchingElement: (int) ele {
    
    // We have take the algorithm and loop all the elements.
    // For linear search the complexity is O(n*m). Here m = 2(2 dimention array) and n = number of elements in the array.
    
    for (int obj1 = 0; obj1 < arr.count; obj1++) {
        NSArray *subArr = arr[obj1] ;
        for (int obj2 = 0; obj2 < subArr.count; obj2++) {
            int value = [subArr[obj2] intValue];
            if (ele == value ) {
                
               // NSDictionary cannot store scalar values (like BOOL, NSInteger, etc.), it can store only objects. You must wrap your scalar values into NSNumber to store them:
                NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys: [NSNumber numberWithInt:obj2], @"SubArrayPositioin", [NSNumber numberWithInt:obj1], @"Position", nil];
                return dic;
            }
        }
    }
    
    return nil;
}
@end


int main() {
    @autoreleasepool {
        NSArray *elementsArray = @[@[@1, @2], @[@10, @20], @[@100, @200], @[@1000, @2000]];
        int searchingElement = 1000;
        
        AlgorithmSearch * algoSearch = [[AlgorithmSearch alloc] init];
        NSDictionary *indexDic = [algoSearch multiDimensionLinearSearch:elementsArray searchingElement:searchingElement];
        NSLog(@"%@",indexDic);
    }
}

The output will be as shown below:

Algo_Linear_ObjectiveC[10305:627287] {

    Position = 3;
    SubArrayPositioin = 0;
}


For reference of the above code you can download the code at this link

No comments:

Post a Comment

SOLID Principles

SOLID principles are the basic essential thing to know for every developer before he/she starts coding in any IDE. Here is the full form S ...