-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterpolation.cs
More file actions
47 lines (35 loc) · 1.27 KB
/
Copy pathInterpolation.cs
File metadata and controls
47 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.Algorithms.Search;
public class Interpolation : ISearchAlgorithm<int>
{
public int Search(int[] target, int value)
{
if (target is null) return default;
if (target.Length == 0) return -1;
if (target.Length == 1) return target[0] == value ? 0 : -1;
var start = 0;
var end = target.Length - 1;
while (end >= start && target[start] <= value && target[end] >= value)
{
if (start == end)
{
if (target[start] == value) return start;
return -1;
}
var position = GetPosition(target, value, start, end);
if (target[position] == value) return position;
if (target[position] > value)
end = position - 1;
else
start = position + 1;
}
return -1;
}
private static int GetPosition(IReadOnlyList<int> target, int targetValue, int start, int end)
{
var xSlope = target[end] - target[start];
var ySlope = end - start;
var difference = targetValue - target[start];
return start + ySlope / xSlope * difference;
}
}