Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions algorithms/CSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ To run the `.cs` file, kindly use [.Net Finddle](https://dotnetfiddle.net/)
- [Binary Search](src/Search/binary-search.cs)
- [Linear Search](src/Search/linear-search.cs)
- [Minima Maxima](src/Search/minima-maxima.cs)
- [Interpolation Search](src/Search/interpolation-search.cs)

## Maths

Expand Down
53 changes: 53 additions & 0 deletions algorithms/CSharp/src/Search/interpolation-search.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithms.Search
{
public class InterpolationSearch
{
public static void Main()
{
var sortedArray = new int[] { 3, 4, 7, 10, 12, 18 };
var item = 4;

var resultIndex = Search(sortedArray, item);
if (resultIndex != -1)
{
Console.WriteLine($"Item {item} was found at index {resultIndex} in the array");
}
else
{
Console.WriteLine($"Item {item} was not found in the array");
}
}

// Array must be sorted
// Returns index of item if it is present in sorted array, else return -1
public static int Search(int[] arr, int item)
{
var left = 0;
var right = arr.Length - 1;

while (item >= arr[left] && item <= arr[right] && left <= right)
{
// Probing index of item
var probe = left + (right - left) * (item - arr[left]) / (arr[right] - arr[left]);
if (item == arr[probe])
{
return probe;
}
if (item < arr[probe])
{
right = probe - 1;
}
else
{
left = probe + 1;
}
}
return -1;
}
}
}
27 changes: 27 additions & 0 deletions algorithms/CSharp/test/Search/interpolation-search.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Algorithms.Search;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithms.Tests.Search
{
[TestFixture]
public class InterpolationSearchTest
{
[TestCase(new int[]{ 1, 2, 3, 4, 5, 6, 7}, 3)]
public void InterpolationSearch_GetIndexOfItem(int[] input, int item)
{
var expected1 = InterpolationSearch.Search(input, item);
Assert.AreEqual(expected1, 2);
}

[TestCase(new int[] { 10, 21, 34, 46, 57, 68 }, 40)]
public void InterpolationSearch_ItemNotFound(int[] input, int item)
{
var expected = InterpolationSearch.Search(input, item);
Assert.AreEqual(expected, -1);
}
}
}
52 changes: 52 additions & 0 deletions docs/en/Searching/Interpolation-Search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Interpolation Search Algorithm

**Interpolation search** is an efficient searching algorithm used for finding a target element in a sorted array. It improves upon binary search by estimating the position of the target element based on its value and the distribution of values in the array.

1. Time Complexity: O(log(log n)) on average, O(n) in the worst case.
2. Space Complexity: O(1).
3. Applications: Used in scenarios where data is uniformly distributed and binary search is not optimal.
4. Founder's Name: Jon Louis Bentley.


## Steps:
1. Initialization: Set low to the index of the first element and high to the index of the last element in the sorted array.
2. Calculate Probe Position: Estimate the position of the probe using the formula:
mid = low + ((target - array[low]) * (high - low)) / (array[high] - array[low])
3. Check if Target Found:
If array[mid] equals the target, return mid.
If array[mid] is less than the target, update low = mid + 1 and repeat step 2.
If array[mid] is greater than the target, update high = mid - 1 and repeat step 2.
4. Repeat or Return: Continue steps 2 and 3 until the target is found or low is greater than high. If low becomes greater than high, the target is not in the array.

## Example:
Suppose we have a sorted array: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100], and we want to search for the element 60.

1. Initialization:

low = 0
high = 9
2. Calculate Probe Position:

mid = 0 + ((60 - 10) * (9 - 0)) / (100 - 10)
= 0 + (50 * 9) / 90
= 0 + 450 / 90
= 5
3. Check if Target Found:
array[mid] = array[5] = 60 (match found)

4. Return index 5 as the position of the target element 60 in the array.

## Implementation

I will add it

## Video URL

[Watch Interpolation Search Algorithm](https://www.youtube.com/watch?v=YSVS5GG1JuI)

## Others

Interpolation search performs well when the elements in the array are uniformly distributed. However, it may not perform optimally if the array has irregularly spaced elements or is heavily weighted towards one end. In such cases, binary search or other algorithms might be more suitable.


## Generated using chatGPT