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

Added Exponential Search and Tests #32

Merged
merged 1 commit into from
Oct 18, 2018
Merged
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
29 changes: 29 additions & 0 deletions Tests/search/ExponentialSearchTests.cs
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 exponentialsearch/ExponentialSearchTests/ExponentialSearchTests.cs
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);
}
}
}
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>
31 changes: 31 additions & 0 deletions exponentialsearch/exponentialsearch.sln
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
80 changes: 80 additions & 0 deletions exponentialsearch/exponentialsearch/ExponentialSearch.cs
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()
{

}
}
}
8 changes: 8 additions & 0 deletions exponentialsearch/exponentialsearch/exponentialsearch.csproj
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>