forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a BinarySearch for Ilist<T> data that is already sorted.
A custom comparer can be provided incase the data is sorted descendingly, the default expectation is that the data is sorted ascendingly.
- Loading branch information
Showing
2 changed files
with
73 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,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace JamLib.Algorithms.Searching | ||
{ | ||
public static class BinarySearch | ||
{ | ||
/// <summary> | ||
/// Finds an elements Index using worst O(lg n) in an already sorted data set | ||
/// </summary> | ||
/// <param name="data">the data needs to be sorted in ascending order, unless a custom comparer is provided</param> | ||
/// <returns>-1 if not found, else the index of the element</returns> | ||
public static int Search<T>(this IList<T> data, T element) { return Search(data, element, Comparer<T>.Default); } | ||
public static int Search<T>(this IList<T> data, T element, IComparer<T> comparer) | ||
{ | ||
// NOTE: Exit early if the element couldn't possibly be in the data | ||
if (data.Count < 1 || comparer.Compare(data[0], element) > 0 || comparer.Compare(data[data.Count - 1], element) < 0) { return -1; } | ||
|
||
int minIndex = 0; | ||
int maxIndex = data.Count - 1; | ||
|
||
while (minIndex <= maxIndex) | ||
{ | ||
int midIndex = (minIndex + maxIndex) / 2; | ||
int c = comparer.Compare(data[midIndex], element); | ||
|
||
if (c == 0) { return midIndex; } // Found | ||
else if (c > 0) { maxIndex = midIndex - 1; } // Left Side | ||
else if (c < 0) { minIndex = midIndex + 1; } // Right Side | ||
} | ||
|
||
// Did not find the Item | ||
return -1; | ||
} | ||
} | ||
} |
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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using JamLib.Algorithms.Searching; | ||
|
||
namespace JamLib.Algorithms.Searching.Tests | ||
{ | ||
[TestClass()] | ||
public class BinarySearchTests | ||
{ | ||
[TestMethod()] | ||
public void SearchTest() | ||
{ | ||
// Default Data needs to be sorted ascending | ||
var data = new int[] { -1, 0, 1, 4, 5, 7, 88, 90, 111, 160, 250 }; | ||
int actualIndex = BinarySearch.Search(data, 90); | ||
int expectedIndex = 7; | ||
|
||
Assert.AreEqual(expectedIndex, actualIndex, "Binary search did not provide the correct index"); | ||
} | ||
|
||
[TestMethod()] | ||
public void Search_CustomComparer_Test() | ||
{ | ||
// This data, is sorted descending, therefore we use a custom comparer | ||
int[] data = new int[] { 12, 10, 7, 6, 5, 5, 4, 2, 1, 0, -4, -24 }; | ||
Comparison<int> descending = ((x, y) => y > x ? 1 : y < x ? -1 : 0); | ||
int actualIndex = BinarySearch.Search(data, 4, Comparer<int>.Create(descending)); | ||
int expectedIndex = 6; | ||
|
||
Assert.AreEqual(expectedIndex, actualIndex, "Binary search did not provide the correct index with a custom comparer"); | ||
} | ||
} | ||
} |