1 Star 0 Fork 63

雁秋 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
FastSearcher.cs 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
using System;
using Utilities.Exceptions;
namespace Algorithms.Search
{
/// <summary>
/// The idea: you could combine the advantages from both binary-search and interpolation search algorithm.
/// Time complexity:
/// worst case: Item couldn't be found: O(log n),
/// average case: O(log log n),
/// best case: O(1).
/// Note: This algorithm is recursive and the array has to be sorted beforehand.
/// </summary>
public class FastSearcher
{
/// <summary>
/// Finds index of first item in array that satisfies specified term
/// throws ItemNotFoundException if the item couldn't be found.
/// </summary>
/// <param name="array">Span of sorted numbers which will be used to find the item.</param>
/// <param name="item">Term to check against.</param>
/// <returns>Index of first item that satisfies term.</returns>
/// <exception cref="ItemNotFoundException"> Gets thrown when the given item couldn't be found in the array.</exception>
public int FindIndex(Span<int> array, int item)
{
if (array.Length == 0)
{
throw new ItemNotFoundException();
}
if (item < array[0] || item > array[^1])
{
throw new ItemNotFoundException();
}
if (array[0] == array[^1])
{
return item == array[0] ? 0 : throw new ItemNotFoundException();
}
var (left, right) = ComputeIndices(array, item);
var (from, to) = SelectSegment(array, left, right, item);
return from + FindIndex(array.Slice(from, to - from + 1), item);
}
private (int left, int right) ComputeIndices(Span<int> array, int item)
{
var indexBinary = array.Length / 2;
int[] section =
{
array.Length - 1,
item - array[0],
array[^1] - array[0],
};
var indexInterpolation = section[0] * section[1] / section[2];
// Left is min and right is max of the indices
return indexInterpolation > indexBinary
? (indexBinary, indexInterpolation)
: (indexInterpolation, indexBinary);
}
private (int from, int to) SelectSegment(Span<int> array, int left, int right, int item)
{
if (item < array[left])
{
return (0, left - 1);
}
if (item < array[right])
{
return (left, right - 1);
}
return (right, array.Length - 1);
}
}
}
C#
1
https://gitee.com/xyesterday/C-Sharp.git
git@gitee.com:xyesterday/C-Sharp.git
xyesterday
C-Sharp
C-Sharp
master

搜索帮助