Skip to content

Commit

Permalink
Implemented a BinarySearch for Ilist<T> data that is already sorted.
Browse files Browse the repository at this point in the history
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
joshimoo committed Aug 20, 2014
1 parent f8a4b5e commit 5e5f619
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Binary_Search/C#/joshimoo/BinarySearch.cs
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;
}
}
}
34 changes: 34 additions & 0 deletions Binary_Search/C#/joshimoo/BinarySearchTests.cs
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");
}
}
}

0 comments on commit 5e5f619

Please sign in to comment.