Skip to content

Commit

Permalink
Add interpolation search (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
BSzmolke committed May 25, 2023
1 parent 83b5443 commit c13608a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
58 changes: 58 additions & 0 deletions Algorithms.Tests/Search/InterpolationSearchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Algorithms.Search;
using NUnit.Framework.Internal;
using NUnit.Framework;
using System;
using System.Linq;

namespace Algorithms.Tests.Search
{
public static class InterpolationSearchTests
{
[Test]
public static void FindIndex_ItemPresent_IndexCorrect([Random(1, 1000, 100)] int n)
{
// Arrange
var random = Randomizer.CreateRandomizer();
var arrayToSearch = Enumerable.Range(0, n).Select(_ => random.Next(0, 1000)).OrderBy(x => x).ToArray();
var selectedIndex = random.Next(0, n);

// Act
var actualIndex = InterpolationSearch.FindIndex(arrayToSearch, arrayToSearch[selectedIndex]);

// Assert
Assert.AreEqual(arrayToSearch[selectedIndex], arrayToSearch[actualIndex]);
}

[Test]
public static void FindIndex_ItemMissing_MinusOneReturned(
[Random(0, 1000, 10)] int n,
[Random(-100, 1100, 10)] int missingItem)
{
// Arrange
var random = Randomizer.CreateRandomizer();
var arrayToSearch = Enumerable.Range(0, n)
.Select(_ => random.Next(0, 1000))
.Where(x => x != missingItem)
.OrderBy(x => x).ToArray();

// Act
var actualIndex = InterpolationSearch.FindIndex(arrayToSearch, missingItem);

// Assert
Assert.AreEqual(-1, actualIndex);
}

[Test]
public static void FindIndex_ArrayEmpty_MinusOneReturned([Random(100)] int itemToSearch)
{
// Arrange
var arrayToSearch = new int[0];

// Act
var actualIndex = InterpolationSearch.FindIndex(arrayToSearch, itemToSearch);

// Assert
Assert.AreEqual(-1, actualIndex);
}
}
}
52 changes: 52 additions & 0 deletions Algorithms/Search/InterpolationSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Algorithms.Search
{
/// <summary>
/// Class that implements interpolation search algorithm.
/// </summary>
public static class InterpolationSearch
{
/// <summary>
/// Finds the index of the item searched for in the array.
/// Algorithm performance:
/// worst-case: O(n),
/// average-case: O(log(log(n))),
/// best-case: O(1).
/// </summary>
/// <param name="sortedArray">Array with sorted elements to be searched in. Cannot be null.</param>
/// <param name="val">Value to be searched for. Cannot be null.</param>
/// <returns>If an item is found, return index, else return -1.</returns>
public static int FindIndex(int[] sortedArray, int val)
{
var start = 0;
var end = sortedArray.Length - 1;

while (start <= end && val >= sortedArray[start] && val <= sortedArray[end])
{
var denominator = (sortedArray[end] - sortedArray[start]) * (val - sortedArray[start]);

if (denominator == 0)
{
denominator = 1;
}

var pos = start + (end - start) / denominator;

if (sortedArray[pos] == val)
{
return pos;
}

if (sortedArray[pos] < val)
{
start = pos + 1;
}
else
{
end = pos - 1;
}
}

return -1;
}
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ find more than one implementation for the same objective but using different alg
* [Searches](./Algorithms/Search)
* [A-Star](./Algorithms/Search/AStar/)
* [Binary Search](./Algorithms/Search/BinarySearcher.cs)
* [Recursive Binary Search](./Algorithms/Search/RecursiveBinarySearcher.cs)
* [Linear Search](./Algorithms/Search/LinearSearcher.cs)
* [BoyerMoore Search](./Algorithms/Search/BoyerMoore.cs)
* [Fast Search](./Algorithms/Search/FastSearcher.cs)
* [Fibonacci Search](./Algorithms/Search/FibonacciSearcher.cs)
* [Interpolation Search](./Algorithms/Search/InterpolationSearch.cs)
* [Jump Search](./Algorithms/Search/JumpSearcher.cs)
* [Linear Search](./Algorithms/Search/LinearSearcher.cs)
* [Recursive Binary Search](./Algorithms/Search/RecursiveBinarySearcher.cs)
* [Sorts](./Algorithms/Sorters)
* [Comparison](./Algorithms/Sorters/Comparison)
* [Binary Insertion Sort](./Algorithms/Sorters/Comparison/BinaryInsertionSorter.cs)
Expand Down

0 comments on commit c13608a

Please sign in to comment.