Golang Program to search an item into the array using interpolation search


In this tutorial, we will see how to search an item in the array using interpolation search. It is very similar to Binary search but is an improved version of it, in this algorithm the element is searched from the sorted array using a different method to find middle element and the output is printed using print function in Golang. Let’s have a look to understand it −

Using interpolation search in main method

In this method, we will learn how to search an element in the array using an interpolation search method. All the things will be executed in the main program and the output will be printed using fmt.Println() function.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Create a function main and further create variables flag, start, end and middle in it and initialize them with the value zero.

Step 3 − Create an array and fill it with respective elements, also create a variable named item which is to be searched inside the array.

Step 4 − Set start equal to zero and end as len(arr)-1 and find middle element using way as suggested in the code.

Step 5 − Run a loop where start<=end and check if the array middle element which is calculated above is less than item then do start = middle+1.

Step 6 − If the middle array element is equal to the item set flag as middle index and break the loop.

Step 7 − If none of the condition satisfies set end=middle-1 and again calculate middle index.

Step 8 − After the loop terminates check if the flag is not equal to zero print that item is present in the array.

Step 9 − If flag is not zero print item is not present in the array. The print statement is executed using fmt.Println() function.

Example

Golang program to search an item into the array using interpolation search in main method

package main
import "fmt"
func main() {

   var flag int = 0
   var start int = 0
   var end int = 0
   var middle int = 0
   
   arr := []int{10, 20, 30, 40, 50}
   fmt.Println("The respective array elements are:", arr)
   var item int = 40
   fmt.Println("The value to be searched is:", item)
   
   start = 0
   end = 4
   middle = start + (((end - start) / (arr[end] - arr[start])) * (item - arr[start]))
   
   for start := 0; start <= end; {
      if arr[middle] < item {
         start = middle + 1
      } else if arr[middle] == item {
         flag = middle
         break
      } else {
         end = middle - 1
      }
      middle = start + (((end - start) / (arr[end] - arr[start])) * (item - arr[start]))
   }
   if flag != 0 {
      fmt.Println("Item", item, "is found at index:", flag, "in the array")
   } else {
      fmt.Println("Item", item, "not found in the array")
   }
}

Output

The respective array elements are: [10 20 30 40 50]
The value to be searched is: 40
Item 40 is found at index: 3 in the array

Using interpolation search in a helper function

In this method, we will see how the element is searched in the array using helper function with the array and the item to be searched as in the array as parameters. Let’s dive into the code to know how it can be solved.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Create a function search with array and item to be searched as parameters and further create variables flag, start, end and middle in it and initialize them with the value zero.

Step 3 − Set start equal to zero and end as len(arr)-1 and find middle element using way as suggested in the code.

Step 4 − Run a loop where start<=end and check if the array middle element which is calculated above is less than item then do start = middle+1.

Step 5 − If the middle array element is equal to the item set flag as middle index and break the loop.

Step 6 − If none of the condition satisfies set end=middle-1 and again calculate middle index.

Step 7 − After the loop terminates check if the flag is not equal to zero print that item is present in the array.

Step 8 − If flag is not zero print item is not present in the array. The print statement is executed using fmt.Println() function.

Step 9 − The user-defined function is called from the main with few arguments.

Example

Golang program to search an item into the array using interpolation search in helper function

package main
import "fmt"
func search(arr []int, item int) {

   var flag int = 0
   var start int = 0
   var end int = 0
   var middle int = 0
   
   start = 0
   end = 4
   middle = start + (((end - start) / (arr[end] - arr[start])) * (item - arr[start]))
   
   for start := 0; start <= end; {
      if arr[middle] < item {
         start = middle + 1
      } else if arr[middle] == item {
         flag = middle
         break
      } else {
         end = middle - 1
      }
      middle = start + (((end - start) / (arr[end] - arr[start])) * (item - arr[start]))
   }
   if flag != 0 {
      fmt.Println("Item", item, "is found at index:", flag, "in the array")
   } else {
      fmt.Println("Item", item, "not found in the array")
   }
}

func main() {
   arr := []int{10, 20, 30, 40, 50}
   fmt.Println("The respective array elements are:", arr)
   var item int = 40
   fmt.Println("The value to be searched is:", item)
   search(arr, item)
}

Output

The respective array elements are: [10 20 30 40 50]
The value to be searched is: 40
Item 40 is found at index: 3 in the array

Conclusion

In the above tutorial, we searched an element in the array using two methods. In the first method we used the main function in which we searched for the element with the improved version of binary search. In the second method we used an external function array and the item to be searched as parameters. Hence, all the methods executed successfully.

Updated on: 13-Feb-2023

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements