Tuesday, 9 June 2015

Algorithms of sequential search and binary search

This article provides an algorithm of sequential search and binary search.

Algorithm for Sequential Search

1.  Initialize searcharray, searchno, length.

2.  Initialize pos=0.

3.  Repeat step 4 till pos<=length.

4.  if searcharray[pos]=searchno
        return pos
    else
        increment pos by 1.

Algorithm for Binary Search

1.  Initialize an ordered array, searcharray, searchno, length.

2.  Initialize low=0 and high=length.

3.  Repeat step 4 till low<=high.

4.  Middle =(low + high) / 2.

5.  if searcharray[middle]=searchno
        Search is successful
        return middle.
    else if searcharray[middle]>searchno[high]
        high=middle - 1
    else
        low=middle + 1.

No comments:

Post a Comment