Searching

Linear/Sequential Search
int? LinearSearch(int[] array, int target){
    int n = array.Length - 1;
    for(int i = 0; i <= n; i++){
        if(array[i] == target)
            return i;
    }
    return -1;
}

Time Complexity: O(n)

  • Binary search works only on a sorted set of elements.

Binary Search
  • Using Recursion:

Time Complexity: O(log n)

Fact

If all the names in the world are written down together in order and you want to search for the position of a specific name, binary search will accomplish this in a maximum of 35 iterations.

Last updated

Was this helpful?