diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index edee912aa..4f979e563 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -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 diff --git a/algorithms/CSharp/src/Search/interpolation-search.cs b/algorithms/CSharp/src/Search/interpolation-search.cs new file mode 100644 index 000000000..e6d551421 --- /dev/null +++ b/algorithms/CSharp/src/Search/interpolation-search.cs @@ -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; + } + } +} diff --git a/algorithms/CSharp/test/Search/interpolation-search.cs b/algorithms/CSharp/test/Search/interpolation-search.cs new file mode 100644 index 000000000..32458a7e1 --- /dev/null +++ b/algorithms/CSharp/test/Search/interpolation-search.cs @@ -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); + } + } +} diff --git a/docs/en/Searching/Interpolation-Search.md b/docs/en/Searching/Interpolation-Search.md new file mode 100644 index 000000000..be1a55ccb --- /dev/null +++ b/docs/en/Searching/Interpolation-Search.md @@ -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