diff --git a/Tests/search/ExponentialSearchTests.cs b/Tests/search/ExponentialSearchTests.cs new file mode 100644 index 00000000..f95f20fc --- /dev/null +++ b/Tests/search/ExponentialSearchTests.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using csharp_algorithms; +using exponentialsearch; + +namespace Tests.search +{ + [TestClass] + public class ExponentialSearchTest + { + [TestMethod] + public void Search() + { + // Setup + int[] arr = { 0, 1, 10, 13, 16, 20, 22, 28, 31, 39, 45, 55 }; + int searchElem = 10; + + + // Search + var result1 = ExponentialSearch.Search(arr, searchElem); + searchElem = 11; + var result2 = ExponentialSearch.Search(arr, searchElem); ; + + // Assert + Assert.AreEqual(true, result1); + Assert.AreEqual(false, result2); + } + } +} \ No newline at end of file diff --git a/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.cs b/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.cs new file mode 100644 index 00000000..a1a1e35a --- /dev/null +++ b/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +//using csharp_algorithms; +using exponentialsearch; + +namespace Tests.search +{ + [TestClass] + public class ExponentialSearchTest + { + [TestMethod] + public void Search() + { + // Setup + int[] arr = { 0, 1, 10, 13, 16, 20, 22, 28, 31, 39, 45, 55 }; + int searchElem = 10; + + + // Search + var result1 = ExponentialSearch.Search(arr, searchElem); + searchElem = 11; + var result2 = ExponentialSearch.Search(arr, searchElem); ; + + // Assert + Assert.AreEqual(true, result1); + Assert.AreEqual(false, result2); + } + } +} \ No newline at end of file diff --git a/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.csproj b/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.csproj new file mode 100644 index 00000000..a0381fad --- /dev/null +++ b/exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + diff --git a/exponentialsearch/exponentialsearch.sln b/exponentialsearch/exponentialsearch.sln new file mode 100644 index 00000000..05848393 --- /dev/null +++ b/exponentialsearch/exponentialsearch.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2046 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "exponentialsearch", "exponentialsearch\exponentialsearch.csproj", "{D0929E76-9CBB-4114-A8E1-DAC86469231A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExponentialSearchTests", "ExponentialSearchTests\ExponentialSearchTests.csproj", "{33278F91-0695-4B3A-A2EA-C31DB15AC0F7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D0929E76-9CBB-4114-A8E1-DAC86469231A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0929E76-9CBB-4114-A8E1-DAC86469231A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0929E76-9CBB-4114-A8E1-DAC86469231A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0929E76-9CBB-4114-A8E1-DAC86469231A}.Release|Any CPU.Build.0 = Release|Any CPU + {33278F91-0695-4B3A-A2EA-C31DB15AC0F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33278F91-0695-4B3A-A2EA-C31DB15AC0F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33278F91-0695-4B3A-A2EA-C31DB15AC0F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33278F91-0695-4B3A-A2EA-C31DB15AC0F7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1CD73586-E288-4293-9D0B-73997DF04E73} + EndGlobalSection +EndGlobal diff --git a/exponentialsearch/exponentialsearch/ExponentialSearch.cs b/exponentialsearch/exponentialsearch/ExponentialSearch.cs new file mode 100644 index 00000000..09eb2568 --- /dev/null +++ b/exponentialsearch/exponentialsearch/ExponentialSearch.cs @@ -0,0 +1,80 @@ +using System; +using System.Diagnostics; + +namespace exponentialsearch +{ + public class ExponentialSearch + { + /** + * Exponential Search method which defines a subset range from the + * given array and looks for the element in it. + */ + public static Boolean Search(int[] arr, int elem) + { + int last = arr.Length - 1; + + if (last <= 0) + return false; + + if (arr[0] == elem) + return true; + + // Find the range in which we will be looking for the element + int i = 1; + while (i < last && arr[i] <= elem) + i *= 2; + + return BinarySearchRecursive(arr, i / 2, Math.Min(i, last), elem); + } + + /** + * Binary Search is implemented through a recursive approach. If the + * the elem is not found at the midIndex, the method is called again + * with readjusted left or right range. + */ + public static Boolean BinarySearchRecursive(int[] arr, int leftRange, int rightRange, + int elem) + { + if (leftRange > rightRange) + return false; + + // Calculate the middle of the range given. This way there is no overflow + // in case of a larger array. For more info: + // https://stackoverflow.com/questions/6735259/calculating-mid-in-binary-search + int midIndex = leftRange + (rightRange - leftRange) / 2; + + // If element found, return index, otherwise adjust left or right range + if (arr[midIndex] == elem) + return true; + else if (elem < arr[midIndex]) + return BinarySearchRecursive(arr, leftRange, midIndex - 1, elem); + else if (elem > arr[midIndex]) + return BinarySearchRecursive(arr, midIndex + 1, rightRange, elem); + return false; + } + + public static Boolean BinarySearchIterative(int[] arr, int leftRange, int rightRange, + int elem) + { + int midIndex = 0; + while (leftRange <= rightRange) + { + midIndex = leftRange + (rightRange - leftRange) / 2; + + if (arr[midIndex] == elem) + return true; + else if (elem < midIndex) + rightRange = midIndex - 1; + else + leftRange = midIndex + 1; + } + + return false; + } + + public static void Main() + { + + } + } +} \ No newline at end of file diff --git a/exponentialsearch/exponentialsearch/exponentialsearch.csproj b/exponentialsearch/exponentialsearch/exponentialsearch.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/exponentialsearch/exponentialsearch/exponentialsearch.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + +