Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search Algoritm with its tests #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Tests/search/BinarySearchTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using csharp_algorithms;
using binarysearch;

namespace Tests.search
{
[TestClass]
public class BinarySearchTest
{
[TestMethod]
public void Test1()
{
var nodes = new List<Node>
{
new Node(10),
new Node(5),
new Node(3),
new Node(50),
new Node(75),
new Node(81),
new Node(8),
new Node(93),
new Node(573),
new Node(111),
new Node(4324),
new Node(5532),
new Node(123),
new Node(1),
new Node(32),
new Node(64),
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 bsearch = new BinarySearch(nodes);

var testValue1 = new Node(1);
var testValue2 = new Node(123123123);
var testValue3 = new Node(81330);

var indexValue1 = bsearch.Search(testValue1);
var indexValue2 = bsearch.Search(testValue2);
var indexValue3 = bsearch.Search(testValue3);

Assert.AreEqual(indexValue1,0);
Assert.IsNull(indexValue2);
Assert.AreEqual(indexValue3, nodes.Count() - 1);
}
}
}
54 changes: 54 additions & 0 deletions binarysearch/BinarySearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using csharp_algorithms;

namespace binarysearch
{
public class BinarySearch
{
private readonly IEnumerable<Node> _list;

public BinarySearch(IEnumerable<Node> list)
{
//Ordering list in ascending order
_list = list.OrderBy(n => n.Number);
}

//This is nullable int because if we can't find the value inside the list
//we will return null in order to notify the user.
public int? Search(Node value)
{
int left = 0;
int right = _list.Count() - 1;
int mid;

//We will loop until the search space does not contain any value
while(left <= right)
{
//we find mid in order to compare it to Node value.
mid = (left + right) / 2;

//If we find our value return it's index
if(value.Number == _list.ElementAt(mid).Number)
{
return mid;
}
//If our number is smaller than the mid value we're going to shrink our array by getting
//our right index next to mid index
else if(value.Number < _list.ElementAt(mid).Number)
{
right = mid - 1;
}
//Same as previous step but since this time out value is bigger than the middle one from the array,
//we are going to search the upper part of the array.
else
{
left = mid + 1;
}
}
//If we got here, we couldn't find our value inside our array.
return null;
}
}
}
36 changes: 36 additions & 0 deletions binarysearch/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;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("binarysearch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("binarysearch")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("229b8895-f269-475d-8549-cf7fba2e8169")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
54 changes: 54 additions & 0 deletions binarysearch/binarysearch.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>{229B8895-F269-475D-8549-CF7FBA2E8169}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>binarysearch</RootNamespace>
<AssemblyName>binarysearch</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="BinarySearch.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>
7 changes: 7 additions & 0 deletions csharp-algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "knapsackproblem", "knapsack
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bellmanFordAlgorithm", "bellmanFordAlgorithm\bellmanFordAlgorithm.csproj", "{6B79C9EC-0219-44AD-B399-CD4046718E66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "binarysearch", "binarysearch\binarysearch.csproj", "{229B8895-F269-475D-8549-CF7FBA2E8169}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -87,6 +89,10 @@ Global
{6B79C9EC-0219-44AD-B399-CD4046718E66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B79C9EC-0219-44AD-B399-CD4046718E66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B79C9EC-0219-44AD-B399-CD4046718E66}.Release|Any CPU.Build.0 = Release|Any CPU
{229B8895-F269-475D-8549-CF7FBA2E8169}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{229B8895-F269-475D-8549-CF7FBA2E8169}.Debug|Any CPU.Build.0 = Debug|Any CPU
{229B8895-F269-475D-8549-CF7FBA2E8169}.Release|Any CPU.ActiveCfg = Release|Any CPU
{229B8895-F269-475D-8549-CF7FBA2E8169}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +108,7 @@ Global
{8087DFD2-673A-499F-B130-0FAE0F31EADC} = {05BBB622-53C5-4866-869B-B45978502CB5}
{8870FC35-4386-43E5-B5EA-1C764E7ADC54} = {05BBB622-53C5-4866-869B-B45978502CB5}
{6B79C9EC-0219-44AD-B399-CD4046718E66} = {05BBB622-53C5-4866-869B-B45978502CB5}
{229B8895-F269-475D-8549-CF7FBA2E8169} = {F61757B3-8F9B-4A56-9800-CCD038241D81}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F2BFC66-A9D6-4C6D-A15A-984F23C11D29}
Expand Down