Our Feeds

Friday, 25 July 2014

Ajith KP

Searching

          "Try to find something by looking or otherwise seeking carefully and thoroughly" is the meaning of searching. Likewise, the computer uses searching to find some thing. There are so many searching algorithms used in computers [^]. The most popular searching techniques are Linear search and Binary search.


          The linear search are of two types,

  • Ordered Linear Search
  • Unordered Linear Search
          The only difference is ordered linear search uses sorted list to search the `key`, but unordered linear search uses unordered list to search `key`.

          Ordered Linear Search : Algorithm

function UNORDERED_LINEAR SEARCH():
          // n => size of list; k => key to be searched; list => array of elements where we want to search key
          i = 0; 
           while i < n and k > list[i]: 
                    i++;
          if(k==list[i]):
                    "Key Found"
          else:
                    "Key Not Found"

          Unordered Linear Search : Algorithm 


function UNORDERED_LINEAR SEARCH():
          // n => size of list; k => key to be searched; list => array of elements where we want to search key
          i = 0; 
           while i < n and k != list[i]: 
                    i++;
          if(k==list[i]):
                    "Key Found"
          else:
                    "Key Not Found"

          Binary Search : Algorithm

function BINARY SEARCH(int first, int last):
          // n => size of list; k => key to be searched; list => array of elements where we want to search key
          if first > last:
                    "Key Not Found"
          else:
                    midpoint = (first+last)/2;
                    if k == list[midpoint]:
                              "Key Found"
                    elif k < list[midpoint]:
                              BINARY SEARCH(first, midpoint-1)
                    else:
                              BINARY SEARCH(midpoint+1, last)

Source Codehttp://terminalcoders.blogspot.com/2014/07/binary-search-in-cpp.html