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

Algo - Big O notation

Let's consider the Linear search. We have know that the complexity of the Linear search algorithm is O(n^m). 

Yes, what is this 'O' is doing there?

Actually the 'O' is the maximum notation of the algorithm. 

For example, if we have [1,3,7, 9, 0]
and we want to find the index of 0. The answer is 4. 
It takes 5 'for' loops to be executed to find the index of 0 in the given array.

Now take the array as below

[[1,3], [2,4], [7,9], [13,15]]

How many for loops we have to do now to find the index of 15?

we have 4 elements in the main array and each element has another 2 child elements. So the complexity takes 4^2.

And this 4^2 is the maximum and it cannot be more than that to find any other element in the given array.

So we denote it as O(4^2)

Finally, we can say that O is the maximum complexity of the algorithm.

-SnehaVIK

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 ...