Interpolation Search and Exponential Search

3. Interpolation Search:-

The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. Binary Search always goes to the middle element to check. On the other hand, interpolation search may go to different locations according to the value of the key being searched. For example, if the value of the key is closer to the last element, interpolation search is likely to start search toward the end side.

To find the position to be searched, it uses following formula. 

pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]

 

Algorithm  


Step1: In a loop, calculate the value of “pos” using the probe position formula.

Step2: If it is a match, return the index of the item, and exit. 

Step3: If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. 

Step4: Repeat until a match is found or the sub-array reduces to zero.

 

 
  

  • Time complexity (if the elements are uniformly distributed):-O(log(log(n)))

  • Time complexity (if numerical values of the keys increase exponentially):-O(n)

 

 

 

4. Exponential Search

Exponential Search also known as finger search, searches for an element in a sorted array by jumping 2^i elements every iteration where i represents the value of loop control variable, and then verifying if the search element is present between last jump and the current jump. 

Algorithm 

Step 1:- Start with value i=1  

Step 2:- Check for condition i < n and Array[i]<=key, where n is the number of elements in the array and key is the element being searched 

Step 3:- Increment value of i in powers of 2, that is, i=i*2 

Step 4:- Keep on incrementing the value of i until the condition is satisfied

Step 5:- Apply binary on the range i/2 till the end of Array - binarySearch(Array, i/2,min(i,n-1))

 

 

 

  • Time complexity :-O(logn)

 

Comments

  1. NYC....got crystal clear understanding of the concept

    ReplyDelete
  2. Thanks for clearing this concept!!!

    ReplyDelete
  3. Great blog
    very interesting information ๐Ÿ‘

    ReplyDelete
  4. Nice๐Ÿ’ซ very easy to understand

    ReplyDelete
  5. Very well explained!!

    ReplyDelete
  6. Terrific Explanation ๐Ÿ‘๐Ÿป

    ReplyDelete
  7. Great Work Man๐Ÿ‘

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Very helpful!!
    Very nicely explained!! Great work!!

    ReplyDelete
  10. Very well explained

    ReplyDelete
  11. Thank you for wonderful information!

    ReplyDelete
  12. Very good explanations all doubt cleared.

    ReplyDelete
  13. Very well explained

    ReplyDelete
  14. Very well explained

    ReplyDelete

Post a Comment

Popular posts from this blog

Binary Search and Jump Search

Introduction