-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters