-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Calidia/exponential-search
Added Exponential Search and Tests
- Loading branch information
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\exponentialsearch\exponentialsearch.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |