mgroves
By: a guest | Apr 25th, 2009 | Syntax:
C# | Size: 5.55 KB | Hits: 280 | Expires: Never
using System;
using MbUnit.Framework;
namespace codekata2___karate_chop
{
[TestFixture]
public class TestChop
{
[Test]
public void test_chop()
{
Assert.
AreEqual(-1, chop
(3,
new int[] { }));
Assert.
AreEqual(-1, chop
(3,
new[] { 1
}));
Assert.
AreEqual(0, chop
(1,
new[] { 1
}));
Assert.
AreEqual(0, chop
(1,
new[] { 1, 3, 5
}));
Assert.
AreEqual(1, chop
(3,
new[] { 1, 3, 5
}));
Assert.
AreEqual(2, chop
(5,
new[] { 1, 3, 5
}));
Assert.
AreEqual(-1, chop
(0,
new[] { 1, 3, 5
}));
Assert.
AreEqual(-1, chop
(2,
new[] { 1, 3, 5
}));
Assert.
AreEqual(-1, chop
(4,
new[] { 1, 3, 5
}));
Assert.
AreEqual(-1, chop
(6,
new[] { 1, 3, 5
}));
Assert.
AreEqual(0, chop
(1,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(1, chop
(3,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(2, chop
(5,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(3, chop
(7,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(-1, chop
(0,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(-1, chop
(2,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(-1, chop
(4,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(-1, chop
(6,
new[] { 1, 3, 5, 7
}));
Assert.
AreEqual(-1, chop
(8,
new[] { 1, 3, 5, 7
}));
}
// recursive
private int chop_recursive(int search, int[] intlist)
{
if(intlist.Length == 0)
{
return -1;
}
int middleindex = (intlist.Length) / 2;
if(intlist[middleindex] == search)
{
return (middleindex);
}
if(intlist.Length == 1)
{
return (-1);
}
if (search > intlist[middleindex])
{
// chop search on 2nd half
int secondhalflength = intlist.Length - middleindex - 1;
int[] secondhalflist
= new int[secondhalflength
];
Array.Copy(intlist, middleindex+1, secondhalflist, 0, secondhalflength);
int result = chop(search, secondhalflist);
if(result != -1)
{
return (1 + middleindex + chop(search, secondhalflist));
}
return (result);
}
// chop search on 1st half
int[] firsthalflist
= new int[middleindex
];
Array.Copy(intlist, 0, firsthalflist, 0, middleindex);
return (chop(search, firsthalflist));
}
// iterative
private int chop_iterative(int search, int[] intlist)
{
if(intlist.Length == 0)
{
return (-1);
}
int middleindex = (intlist.Length) / 2;
int mindex = 0;
int maxdex = intlist.Length - 1;
while (true)
{
if (search == intlist[middleindex])
{
return (middleindex);
}
if((maxdex-mindex)<=1 && (search!=intlist[middleindex]))
{
return (-1);
}
if (search > intlist[middleindex])
{
mindex = middleindex + 1;
maxdex = intlist.Length - 1;
}
else
{
mindex = 0;
maxdex = middleindex;
}
middleindex = mindex + ((maxdex - mindex)/2);
}
}
// single comparison per iteration
// from: http://en.wikipedia.org/wiki/Binary_search#Implementations
private int chop_single_compare(int search, int[] intlist)
{
int low = 0;
int high = intlist.Length;
while(low < high)
{
int mid = low + ((high - low)/2);
if (intlist[mid] < search)
{
low = mid + 1;
}
else
{
high = mid;
}
}
if ((low < intlist.Length) && (intlist[low] == search))
{
return (low);
}
return (-1);
}
// cheating implementation using framework built in Array binarysearch
private int chop_cheat(int search, int[] intlist)
{
int result = Array.BinarySearch(intlist, search);
if(result < 0)
{
result = -1;
}
return (result);
}
// variation on single compare with max two compares per iteration
// from: http://en.wikibooks.org/wiki/Algorithm_implementation/Search/Binary_search#C.23_.28common_Algorithm.29
private int chop(int search, int[] intlist)
{
int low = 0;
int high = intlist.Length - 1;
while(low <= high)
{
int midpoint = (low + high)/2;
if(search == intlist[midpoint])
{
return (midpoint);
}
if (search < intlist[midpoint])
{
high = midpoint - 1;
}
else
{
low = midpoint + 1;
}
}
return -1;
}
}
}