diff --git a/Tests/search/BinarySearchTest.cs b/Tests/search/BinarySearchTest.cs new file mode 100644 index 00000000..9a57873f --- /dev/null +++ b/Tests/search/BinarySearchTest.cs @@ -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 + { + 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); + } + } +} diff --git a/binarysearch/BinarySearch.cs b/binarysearch/BinarySearch.cs new file mode 100644 index 00000000..789200bc --- /dev/null +++ b/binarysearch/BinarySearch.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using csharp_algorithms; + +namespace binarysearch +{ + public class BinarySearch + { + private readonly IEnumerable _list; + + public BinarySearch(IEnumerable 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; + } + } +} diff --git a/binarysearch/Properties/AssemblyInfo.cs b/binarysearch/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..0412734f --- /dev/null +++ b/binarysearch/Properties/AssemblyInfo.cs @@ -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")] diff --git a/binarysearch/binarysearch.csproj b/binarysearch/binarysearch.csproj new file mode 100644 index 00000000..04c27279 --- /dev/null +++ b/binarysearch/binarysearch.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {229B8895-F269-475D-8549-CF7FBA2E8169} + Library + Properties + binarysearch + binarysearch + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + {3511528e-8696-40b8-85ab-97456347a497} + csharp-algorithms + + + + \ No newline at end of file diff --git a/csharp-algorithms.sln b/csharp-algorithms.sln index 34e32858..e4e4f8ab 100644 --- a/csharp-algorithms.sln +++ b/csharp-algorithms.sln @@ -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 @@ -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 @@ -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}