Search code examples
arrayslistbinary-searchlinear-search

How to Develop algorithms for linear search and binary search using Pseudo code.?


Searching on an array/list is to find a given element on the array and return whether it is found or not and return its position if found. Linear search and binary search are two popular searching algorithms on arrays.

  • Define what an algorithm is and outline the characteristics of a good algorithm. Develop algorithms for linear search and binary search using Pseudo code.

Solution

  • Algorithm

    An algorithmic program is a list of rules to follow in order to solve an issue. It is ordinarily used for processing, calculation and alternative connected computer and mathematical operations.

    The characteristics of a good algorithm.

    1. Finiteness: an algorithm should terminate infinite number of steps and each step must finish in finite amount of time.
    2. Input: An algorithm must have Zero or more but must be finite number of inputs.
    3. Output: An algorithm must have at-least one desirable outcome.
    4. Effectiveness: An algorithm should be effective means that each step should be referred as principle and should be executing in finite time.
    5. Unambiguous: Algorithm should be clear and unambiguous. Each of its steps, and their inputs/outputs should be clear and must lead to only one meaning.
    6. Finiteness: Algorithms must terminate after a finite number of steps.

    Liner Search

    Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found. Pseudocode for Liner Search

    Read size,array[size], search from user
        i=0
    While i<size
            IF 
                search==array[i]
                write i
                break;
            Else
                i++
            Endif
    Endwhile
    

    Binary search

    Binary search is the most popular Search algorithm. It is efficient and also one of the most commonly used techniques that is used to solve problems.

    Pseudo code for Binary Search

    Procedure binary search
       a← sorted array
       b← size of array
       c← value to be searched
    
       Set lowerBound = 1
       Set upperBound = b
    
       while c not found
          if upperBound < lowerBound 
             EXIT: c does not exists.
       
          set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
          
          if a[midPoint] < c
             set lowerBound = midPoint + 1
             
          if a[midPoint] > c
             set upperBound = midPoint - 1 
    
          if a[midPoint] = c
             EXIT: c found at location midPoint
       end while
       
    end procedure