Home »
VB.Net »
VB.Net Programs
VB.Net program to search an item in array using interpolation search
By Nidhi Last Updated : November 16, 2024
Search an item in array using interpolation search
Here, we will create an array of integers and then read elements from the user, and search item in the array using binary search. The interpolation search is used to search item into the sorted array, it is similar to binary search but calculation to find 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 array using interpolation search is given below. The given program is compiled and executed successfully.
VB.Net code to search an item in array using interpolation search
'VB.Net program to search an item in array using
'interpolation search.
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim item As Integer = 0
Dim flag As Integer = 0
Dim first As Integer = 0
Dim last As Integer = 0
Dim middle As Integer = 0
Dim i As Integer = 0
Console.WriteLine("Enter array elements: ")
For i = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("Enter item for searching: ")
item = Integer.Parse(Console.ReadLine())
first = 0
last = 4
middle = (first + last) / 2
While first <= last
middle = first + (((last - first) / (arr(last) - arr(first))) * (item - arr(first)))
If (arr(middle) = item) Then
flag = middle
GoTo out
Else
If (arr(middle) < item) Then
first = middle + 1
Else
last = middle - 1
End If
End If
End While
out:
If flag <> 0 Then
Console.WriteLine("Item found at index {0} in array", flag)
Else
Console.WriteLine("Item is not found")
End If
End Sub
End Module
Output
Enter array elements:
Element[0]: 11
Element[1]: 22
Element[2]: 33
Element[3]: 44
Element[4]: 55
Enter item for searching:
22
Item found at index 1 in array
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains a function Main(). In the Main() we created an array arr of five elements and five variables item, flag, first , last, and middle that are initialized with 0.
Console.WriteLine("Enter array elements: ")
For i = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
Next
In the above code, we read the elements of the array from the user.
Console.WriteLine("Enter item for searching: ")
item = Integer.Parse(Console.ReadLine())
first = 0
last = 4
middle = (first + last) / 2
While first <= last
middle = first + (((last - first) / (arr(last) - arr(first))) * (item - arr(first)))
If (arr(middle) = item) Then
flag = middle
GoTo out
Else
If (arr(middle) < item) Then
first = middle + 1
Else
last = middle - 1
End If
End If
End While
out:
If flag <> 0 Then
Console.WriteLine("Item found at index {0} in array", flag)
Else
Console.WriteLine("Item is not found")
End If
In the above code, we used interpolation search technique; it is the improved version of binary search. Here, we used different formula to calculate middle element.
VB.Net Array Programs »