1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
Algorithms.Tests
Algorithms
Crypto
DataCompression
Encoders
Graph
Knapsack
LinearAlgebra
ModularArithmetic
Numeric
Other
Problems
Search
AStar
BinarySearcher.cs
BoyerMoore.cs
FastSearcher.cs
FibonacciSearcher.cs
InterpolationSearch.cs
JumpSearcher.cs
LinearSearcher.cs
RecursiveBinarySearcher.cs
Sequences
Shufflers
Sorters
Stack
Strings
Algorithms.csproj
NewtonSquareRoot.cs
DataStructures.Tests
DataStructures
Utilities.Tests
Utilities
.editorconfig
.gitignore
C-Sharp.sln
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
stylecop.json
stylecop.ruleset
克隆/下载
FibonacciSearcher.cs 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
using System;
namespace Algorithms.Search;
/// <summary>
/// Class that implements Fibonacci search algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class FibonacciSearcher<T> where T : IComparable<T>
{
/// <summary>
/// Finds the index of the item searched for in the array.
/// Time complexity:
/// worst-case: O(log n),
/// average-case: O(log n),
/// best-case: O(1).
/// </summary>
/// <param name="array">Sorted array to be searched in. Cannot be null.</param>
/// <param name="item">Item to be searched for. Cannot be null.</param>
/// <returns>If an item is found, return index. If the array is empty or an item is not found, return -1.</returns>
/// <exception cref="ArgumentNullException">Gets thrown when the given array or item is null.</exception>
public int FindIndex(T[] array, T item)
{
if (array is null)
{
throw new ArgumentNullException("array");
}
if (item is null)
{
throw new ArgumentNullException("item");
}
var arrayLength = array.Length;
if (arrayLength > 0)
{
// find the smallest Fibonacci number that equals or is greater than the array length
var fibonacciNumberBeyondPrevious = 0;
var fibonacciNumPrevious = 1;
var fibonacciNum = fibonacciNumPrevious;
while (fibonacciNum <= arrayLength)
{
fibonacciNumberBeyondPrevious = fibonacciNumPrevious;
fibonacciNumPrevious = fibonacciNum;
fibonacciNum = fibonacciNumberBeyondPrevious + fibonacciNumPrevious;
}
// offset to drop the left part of the array
var offset = -1;
while (fibonacciNum > 1)
{
var index = Math.Min(offset + fibonacciNumberBeyondPrevious, arrayLength - 1);
switch (item.CompareTo(array[index]))
{
// reject approximately 1/3 of the existing array in front
// by moving Fibonacci numbers
case > 0:
fibonacciNum = fibonacciNumPrevious;
fibonacciNumPrevious = fibonacciNumberBeyondPrevious;
fibonacciNumberBeyondPrevious = fibonacciNum - fibonacciNumPrevious;
offset = index;
break;
// reject approximately 2/3 of the existing array behind
// by moving Fibonacci numbers
case < 0:
fibonacciNum = fibonacciNumberBeyondPrevious;
fibonacciNumPrevious = fibonacciNumPrevious - fibonacciNumberBeyondPrevious;
fibonacciNumberBeyondPrevious = fibonacciNum - fibonacciNumPrevious;
break;
default:
return index;
}
}
// check the last element
if (fibonacciNumPrevious == 1 && item.Equals(array[^1]))
{
return arrayLength - 1;
}
}
return -1;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助