Skip to content

Instantly share code, notes, and snippets.

@KristofferK
Last active February 11, 2018 23:56
Show Gist options
  • Save KristofferK/df4accfa8b628f1c0757ff1235c610d6 to your computer and use it in GitHub Desktop.
Save KristofferK/df4accfa8b628f1c0757ff1235c610d6 to your computer and use it in GitHub Desktop.
Checks if a given array contains the specified item using divide & conquer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
private static ContainsCheckerDnQ<int> containsChecker = new ContainsCheckerDnQ<int>();
private static SortedContainsCheckerDnQ<int> sortedContainsChecker = new SortedContainsCheckerDnQ<int>();
static void Main(string[] args)
{
int[] items = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
PrintContains(7, items, containsChecker);
PrintContains(5, items, containsChecker);
PrintContains(3, items, containsChecker);
PrintContains(-1, items, containsChecker);
PrintContains(0, items, containsChecker);
PrintContains(10, items, containsChecker);
PrintContains(11, items, containsChecker);
PrintContains(7, items, sortedContainsChecker);
PrintContains(5, items, sortedContainsChecker);
PrintContains(3, items, sortedContainsChecker);
PrintContains(-1, items, sortedContainsChecker);
PrintContains(0, items, sortedContainsChecker);
PrintContains(1, items, sortedContainsChecker);
PrintContains(10, items, sortedContainsChecker);
PrintContains(11, items, sortedContainsChecker);
}
private static void PrintContains(int item, int[] items, IContainsChecker<int> checker)
{
Console.WriteLine("Checking " + item);
var exists = checker.Contains(item, items);
Console.WriteLine(exists ?
"It's there!" :
"It isn't there!");
Console.WriteLine("\n\n");
}
}
interface IContainsChecker<T> where T : IComparable
{
bool Contains(T item, T[] items);
}
class ContainsCheckerDnQ<T> : IContainsChecker<T> where T : IComparable
{
public bool Contains(T item, T[] items)
{
Console.WriteLine($"Checking if {item} exists in {string.Join(",", items)}");
if (items.Length == 0) return false;
if (items.Length == 1) return items[0].CompareTo(item) == 0;
var left = new T[items.Length / 2];
var right = new T[items.Length - left.Length];
for (int i = 0; i < left.Length; i++)
{
left[i] = items[i];
}
for (int i = 0; i < right.Length; i++)
{
right[i] = items[left.Length + i];
}
return Contains(item, left) || Contains(item, right);
}
}
class SortedContainsCheckerDnQ<T> : IContainsChecker<T> where T : IComparable
{
public bool Contains(T item, T[] items)
{
return ContainsHelper(item, items, 0, items.Length);
}
private bool ContainsHelper(T item, T[] items, int min, int max)
{
if (min > max) return false;
if (min == max && min == items.Length) return items[min - 1].CompareTo(item) == 0;
var mid = (max + min) / 2;
var comparingElement = items[mid];
var comp = item.CompareTo(comparingElement);
if (comp == -1)
{
return ContainsHelper(item, items, min, mid - 1);
}
else if (comp == 1)
{
return ContainsHelper(item, items, mid + 1, max);
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment