ADVERTISEMENT
ADVERTISEMENT

Golang program to search an item in the array using interpolation search

Here, we are going to learn how to search an item in the array using interpolation search in Golang (Go Language)?
Submitted by Nidhi, on March 07, 2021

Problem Solution:

In this program, we will create an integer array and read elements from the user. Then we will search the given item in an array using interpolation search and print the appropriate message on the console screen.

Interpolation Search: The interpolation search is used to search items into the sorted array, it is similar to binary search but the calculation to find the middle element is different. We can say that it is the improved version of binary search.

Program/Source Code:

The source code to search an item in the array using interpolation search is given below. The given program is compiled and executed successfully.

// Golang program to search an item in the array
// using interpolation search

package main

import "fmt"

func main() {
	var arr [5]int
	var item int = 0
	var flag int = 0
	var first int = 0
	var last int = 0
	var middle int = 0

	fmt.Printf("Enter array elements: \n")
	for i := 0; i <= 4; i++ {
		fmt.Printf("Elements: arr[%d]: ", i)
		fmt.Scanf("%d", &arr[i])
	}

	fmt.Printf("Enter item: ")
	fmt.Scanf("%d", &item)

	first = 0
	last = 4
	middle = first + (((last - first) / (arr[last] - arr[first])) * (item - arr[first]))

	for first := 0; first <= last; {
		if arr[middle] < item {
			first = middle + 1
		} else if arr[middle] == item {
			flag = middle
			break
		} else {
			last = middle - 1
		}
		middle = first + (((last - first) / (arr[last] - arr[first])) * (item - arr[first]))
	}

	if flag != 0 {
		fmt.Printf("Item %d found at index %d", item, flag)
	} else {
		fmt.Printf("Item %d not found in array")
	}
}

Output:

Enter array elements:
Enter array elements:
Elements: arr[0]: 12
Elements: arr[1]: 34
Elements: arr[2]: 56
Elements: arr[3]: 78
Elements: arr[4]: 97
Enter item: 56
Item 56 found at index 2

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created an array arr and read elements from the user. Then we searched items in an array using interpolation search. After that, If the item is found in the array then print the index of an item otherwise print the "Item not found in array" message on the console screen.

Golang Array Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.