diff --git a/algorithms/CSharp/Algorithms.sln b/algorithms/CSharp/Algorithms.sln index b1f09671..d209e28b 100644 --- a/algorithms/CSharp/Algorithms.sln +++ b/algorithms/CSharp/Algorithms.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26124.0 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31515.178 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms", "src\Algorithms.csproj", "{EDD3C753-58A5-4844-81EA-3E9BD6A1D2F6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Algorithms", "src\Algorithms.csproj", "{EDD3C753-58A5-4844-81EA-3E9BD6A1D2F6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Tests", "test\Algorithms.Tests.csproj", "{8E5D96D1-0618-4A33-9AF6-95D1D8F14E5A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Algorithms.Tests", "test\Algorithms.Tests.csproj", "{8E5D96D1-0618-4A33-9AF6-95D1D8F14E5A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -16,9 +16,6 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {EDD3C753-58A5-4844-81EA-3E9BD6A1D2F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EDD3C753-58A5-4844-81EA-3E9BD6A1D2F6}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -45,4 +42,10 @@ Global {8E5D96D1-0618-4A33-9AF6-95D1D8F14E5A}.Release|x86.ActiveCfg = Release|Any CPU {8E5D96D1-0618-4A33-9AF6-95D1D8F14E5A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1E2EF90C-20D0-42F0-9EC4-13C6B0374477} + EndGlobalSection EndGlobal diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index b58f6b80..9273af3b 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -12,6 +12,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ ## Search - [Binary Search](src/Search/binary-search.cs) +- [Linear Search](src/Search/linear-search.cs) ## Maths - [Abundant Number](src/Maths/abundant-number.cs) diff --git a/algorithms/CSharp/src/Search/linear-search.cs b/algorithms/CSharp/src/Search/linear-search.cs new file mode 100644 index 00000000..f2795501 --- /dev/null +++ b/algorithms/CSharp/src/Search/linear-search.cs @@ -0,0 +1,36 @@ +using System; + +namespace Algorithms.Search +{ + public class LinearSearch + { + public static void Main() + { + int[] numbers = { 1, 2, 3, 4, 5 }; + int searchNumber = 3; + int indexOfSearchNumber = Search(numbers, searchNumber); + + if (indexOfSearchNumber == -1) + { + Console.WriteLine("The number you are searching for does not exist in this list."); + } + else + { + Console.WriteLine("The number you are searching for is at index: " + indexOfSearchNumber); + } + } + public static int Search(int[] numbers, int searchNumber) + { + int indexOfSearchNumber = -1; + + for(int index = 0; index < numbers.Length; index++) + { + if (numbers[index] == searchNumber) + { + indexOfSearchNumber = index; + } + } + return indexOfSearchNumber; + } + } +} diff --git a/algorithms/CSharp/test/Search/linear-search.cs b/algorithms/CSharp/test/Search/linear-search.cs new file mode 100644 index 00000000..9c86d17f --- /dev/null +++ b/algorithms/CSharp/test/Search/linear-search.cs @@ -0,0 +1,27 @@ +using NUnit.Framework; +using Algorithms.Search; + +namespace Algorithms.Tests.Search +{ + [TestFixture] + class LinearSearchTest + { + int[] numbers = { 1, 2, 3, 4, 5 }; + + [TestCase(4, 3)] + [TestCase(5, 4)] + public void GivenASearchNumber_ShouldGetMatchingIndexInList(int searchNumber, int expectedIndexOfSearchTerm) + { + int actualIndexOfSearchTerm = LinearSearch.Search(numbers, searchNumber); + Assert.AreEqual(expectedIndexOfSearchTerm, actualIndexOfSearchTerm); + } + + [TestCase(6)] + [TestCase(0)] + public void GivenASearchNumberNotInTheList_ShouldReturnNegativeOne(int searchNumber) + { + int actualIndexOfSearchTerm = LinearSearch.Search(numbers, searchNumber); + Assert.AreEqual(-1, actualIndexOfSearchTerm); + } + } +}