Skip to content

Commit

Permalink
Merge pull request #26 from Dersei/master
Browse files Browse the repository at this point in the history
Added Fibonacci Search
  • Loading branch information
bashbers authored Oct 16, 2018
2 parents e305157 + bce3511 commit 20bad6a
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Tests/FibonacciSearchTest.cs
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);
}
}
}
5 changes: 5 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="FibonacciSearchTest.cs" />
<Compile Include="search\JumpSearchTest.cs" />
<Compile Include="search\LinearsearchTest.cs" />
<Compile Include="search\InterpolationSearchTest.cs" />
Expand Down Expand Up @@ -97,6 +98,10 @@
<Project>{3511528E-8696-40B8-85AB-97456347A497}</Project>
<Name>csharp-algorithms</Name>
</ProjectReference>
<ProjectReference Include="..\fibonaccisearch\fibonaccisearch.csproj">
<Project>{92DB864E-DF8A-470C-9746-69B7C41AB80C}</Project>
<Name>fibonaccisearch</Name>
</ProjectReference>
<ProjectReference Include="..\quicksort\quicksort.csproj">
<Project>{9199c1cc-0966-40c9-8acc-7ab1baf6512a}</Project>
<Name>quicksort</Name>
Expand Down
7 changes: 7 additions & 0 deletions csharp-algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "search", "search", "{F61757
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "selectionsort", "selectionsort\selectionsort.csproj", "{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fibonaccisearch", "fibonaccisearch\fibonaccisearch.csproj", "{92DB864E-DF8A-470C-9746-69B7C41AB80C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "heapsort", "sort\heapsort\heapsort.csproj", "{CEAB3173-99C9-4F09-AEE2-D31152D4270D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "quicksort", "quicksort\quicksort.csproj", "{9199C1CC-0966-40C9-8ACC-7AB1BAF6512A}"
Expand Down Expand Up @@ -53,6 +55,10 @@ Global
{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Release|Any CPU.Build.0 = Release|Any CPU
{92DB864E-DF8A-470C-9746-69B7C41AB80C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92DB864E-DF8A-470C-9746-69B7C41AB80C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DB864E-DF8A-470C-9746-69B7C41AB80C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DB864E-DF8A-470C-9746-69B7C41AB80C}.Release|Any CPU.Build.0 = Release|Any CPU
{CEAB3173-99C9-4F09-AEE2-D31152D4270D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEAB3173-99C9-4F09-AEE2-D31152D4270D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEAB3173-99C9-4F09-AEE2-D31152D4270D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -88,6 +94,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{C557EF5B-AEFE-407B-8D99-43B9EA625B71} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
{A9022928-E9E9-45BE-BFEC-85A1A870A7EB} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
{92DB864E-DF8A-470C-9746-69B7C41AB80C} = {F61757B3-8F9B-4A56-9800-CCD038241D81}
{CEAB3173-99C9-4F09-AEE2-D31152D4270D} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
{9199C1CC-0966-40C9-8ACC-7AB1BAF6512A} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
{C9666973-0DD4-4CBB-BAB7-C54A7299CC0B} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
Expand Down
67 changes: 67 additions & 0 deletions fibonaccisearch/FibonacciSearch.cs
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;
}
}
}
36 changes: 36 additions & 0 deletions fibonaccisearch/Properties/AssemblyInfo.cs
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")]
54 changes: 54 additions & 0 deletions fibonaccisearch/fibonaccisearch.csproj
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>

0 comments on commit 20bad6a

Please sign in to comment.