-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Dersei/master
Added Fibonacci Search
- Loading branch information
Showing
6 changed files
with
232 additions
and
0 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,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using csharp_algorithms; | ||
using fibonaccisearch; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Tests | ||
{ | ||
[TestClass] | ||
public class FibonacciSearchTest | ||
{ | ||
[TestMethod] | ||
public void TestMethod1() | ||
{ | ||
var nodes = new List<Node> | ||
{ | ||
new Node(1), | ||
new Node(21), | ||
new Node(13), | ||
new Node(91), | ||
new Node(100), | ||
new Node(81), | ||
new Node(42), | ||
new Node(12), | ||
new Node(11), | ||
new Node(211), | ||
new Node(113), | ||
new Node(911), | ||
new Node(10), | ||
new Node(811), | ||
new Node(412), | ||
new Node(2), | ||
new Node(83), | ||
new Node(43), | ||
new Node(23), | ||
new Node(131), | ||
new Node(231), | ||
new Node(133), | ||
new Node(913), | ||
new Node(1003), | ||
new Node(81330), | ||
new Node(4233), | ||
new Node(233), | ||
new Node(1033) | ||
}; | ||
|
||
var fibonacciSearch = new FibonacciSearch(nodes); | ||
|
||
var testValueTrue = new Node(42); | ||
var testValueFalse = new Node(111); | ||
var testValueLastPosition = new Node(81330); | ||
|
||
var resultTrue = fibonacciSearch.Search(testValueTrue); | ||
var resultFalse = fibonacciSearch.Search(testValueFalse); | ||
var resultLastPosition = fibonacciSearch.Search(testValueLastPosition); | ||
|
||
Assert.IsTrue(resultTrue); | ||
Assert.IsFalse(resultFalse); | ||
Assert.IsTrue(resultLastPosition); | ||
} | ||
} | ||
} |
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
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
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,67 @@ | ||
using csharp_algorithms; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace fibonaccisearch | ||
{ | ||
public class FibonacciSearch | ||
{ | ||
private readonly IEnumerable<Node> _list; | ||
|
||
public FibonacciSearch(IEnumerable<Node> list) | ||
{ | ||
//Ordering list in ascending order | ||
_list = list.OrderBy(n => n.Number); | ||
} | ||
|
||
|
||
public bool Search(Node value) | ||
{ | ||
var n = _list.Count(); | ||
|
||
//Setting first three Fibonacci numbers | ||
var fib1 = 0; | ||
var fib2 = 1; | ||
var fibSum = 1; | ||
|
||
//Searching for the smallest number in Fibonacci sequence that is equal or greater than n | ||
while (fibSum < n) | ||
{ | ||
fib2 = fib1; | ||
fib1 = fibSum; | ||
fibSum = fib1 + fib2; | ||
} | ||
|
||
//Setting starting position for searching | ||
var offset = -1; | ||
|
||
//Searching as long as there are still items in list | ||
while (fibSum > 1) | ||
{ | ||
//Checking if index is in bounds of list | ||
int i = Math.Min(offset + fib2, n - 1); | ||
|
||
//If value at given index is lower or greater than searched one, the offset is updated | ||
if (_list.ElementAt(i).Number < value.Number) | ||
{ | ||
fibSum = fib1; | ||
fib1 = fib2; | ||
fib2 = fibSum - fib1; | ||
offset = i; | ||
} | ||
else if (_list.ElementAt(i).Number > value.Number) | ||
{ | ||
fibSum = fib2; | ||
fib1 = fib1 - fib2; | ||
fib2 = fibSum - fib1; | ||
} | ||
//If values match methods returns true | ||
else return true; | ||
} | ||
return false; | ||
} | ||
} | ||
} |
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,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// Ogólne informacje o zestawie są kontrolowane poprzez następujący | ||
// zestaw atrybutów. Zmień wartości tych atrybutów, aby zmodyfikować informacje | ||
// powiązane z zestawem. | ||
[assembly: AssemblyTitle("fibonaccisearch")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("fibonaccisearch")] | ||
[assembly: AssemblyCopyright("Copyright © 2018")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Ustawienie elementu ComVisible na wartość false sprawia, że typy w tym zestawie są niewidoczne | ||
// dla składników COM. Jeśli potrzebny jest dostęp do typu w tym zestawie z | ||
// COM, ustaw wartość true dla atrybutu ComVisible tego typu. | ||
[assembly: ComVisible(false)] | ||
|
||
// Następujący identyfikator GUID jest identyfikatorem biblioteki typów w przypadku udostępnienia tego projektu w modelu COM | ||
[assembly: Guid("92db864e-df8a-470c-9746-69b7c41ab80c")] | ||
|
||
// Informacje o wersji zestawu zawierają następujące cztery wartości: | ||
// | ||
// Wersja główna | ||
// Wersja pomocnicza | ||
// Numer kompilacji | ||
// Rewizja | ||
// | ||
// Możesz określić wszystkie wartości lub użyć domyślnych numerów kompilacji i poprawki | ||
// przy użyciu symbolu „*”, tak jak pokazano poniżej: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
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,54 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{92DB864E-DF8A-470C-9746-69B7C41AB80C}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>fibonaccisearch</RootNamespace> | ||
<AssemblyName>fibonaccisearch</AssemblyName> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="FibonacciSearch.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\csharp-algorithms\csharp-algorithms.csproj"> | ||
<Project>{3511528E-8696-40B8-85AB-97456347A497}</Project> | ||
<Name>csharp-algorithms</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |