Linear Search and Binary Search - Coders Funda - Online Tutorials Point Linear Search and Binary Search

Linear Search and Binary Search



Linear Search

static int Linear-search(int array[], int array-size, int Element-to-search)
    {

        for (int i = 0; i < array-size; i++)
            {

            // Return the index of the element if the element
            // is found

            if (arr[i] == x)
                return i;
        }

        // return -1 if the element is not found

        return -1;
    } 


The time complexity of above algorithm is O(n)

Q )  Search Element in unsorted Array 

sol :- 

using System;  
  
class GFG  
{  
    public static int search(int[] arr, int x) 
    { 
        int n = arr.Length; 
        for(int i = 0; i < n; i++) 
        { 
            if(arr[i] == x) 
                return i; 
        } 
        return -1; 
    } 
      
    public static void Main() 
    { 
        int[] arr = { 2, 3, 4, 10, 40 };  
        int x = 10; 
          
        int result = search(arr, x); 
        if(result == -1) 
            Console.WriteLine("Element is not present in array"); 
        else
            Console.WriteLine("Element is present at index "+ result); 
    } 



--------------------------------------------------------------------------------------------------------------

Binary Search

Below are the Iterative implementation of Binary Search :-

static int Binary-Search(int array[], int Element-to-Search) 
    { 



        int low = 0, high = arr.length - 1; 

//low is first index of array and high is last index of array


        while (low <= high) 
       { 
            int mid = l ow+ (high - low) / 2; // Formula to find the mid point
  
            // Check if Element-to-Search is present at mid 

            if (array[mid] == Element-to-Search
                return mid; 
  
            // If Element-to-Search greater, ignore left half 

            if (array[mid] < Element-to-Search
                low = mid + 1; 
  
            // If Element-to-Search is smaller, ignore right half 

            else

                high = mid - 1; 
        } 
  
        // if we reach here, then element was 
        // not present 

        return -1; 

    } 

--------------------------------------------------------------------------------------------------
Recursive implementation of Binary Search


static int binary-Search (int array[], int low, int high, int Element) 
    { 
        if (low <= high) 
             { 
                  int mid = l ow+ (high - low) / 2; 
  
            // If the element is present at the 
            // middle itself 

            if (array[mid] == Element) 
                return mid; 
  
            // If element is smaller than mid, then 
            // it can only be present in left subarray 

            if (array[mid] > Element) 
                return binarySearch(array, low, mid - 1, Element); 
  
            // Else the element can only be present 
            // in right subarray 

            return binarySearch(array, mid + 1, high, Element); 
        } 
  
        // We reach here when element is not present 
        // in array 

        return -1; 

    }