public class Cloud : IComparable<Cloud>, IEquatable<Cloud>
public Cloud(CloudType type)
public int CompareTo(Cloud obj)
if ((int)this.Type == (int)obj.Type)
if ((int)this.Type < (int)obj.Type)
if ((int)this.Type > (int)obj.Type)
public bool Equals(Cloud other)
if (ReferenceEquals(null, other))
if (ReferenceEquals(this, other))
return (int)Type == (int)other.Type;
public override bool Equals(object obj)
if (ReferenceEquals(null, obj))
if (ReferenceEquals(this, obj))
if (obj.GetType() != this.GetType())
return Equals((Cloud)obj);
public override int GetHashCode()
static int RecursiveBinarySearch<T>(T[] array, T target, int? mostLeftIndex = null, int? mostRightIndex = null)
where T : IComparable<T>, IEquatable<T>
if (!mostLeftIndex.HasValue)
if (!mostRightIndex.HasValue)
mostRightIndex = array.Length - 1;
if (mostRightIndex < mostLeftIndex)
var middleIndex = (int)(mostLeftIndex + (mostRightIndex - mostLeftIndex) / 2);
if (array[middleIndex].CompareTo(target) == 0)
return target.CompareTo(array[middleIndex]) < 0
? RecursiveBinarySearch(array, target, mostLeftIndex, middleIndex - 1)
: RecursiveBinarySearch(array, target, middleIndex + 1, mostRightIndex);
static void Main(string[] args)
var sortedArray = new Cloud[]{new Cloud(Cloud.CloudType.Level10), new Cloud(Cloud.CloudType.Level20), new Cloud(Cloud.CloudType.Level30), new Cloud(Cloud.CloudType.Level40), new Cloud(Cloud.CloudType.Level50), new Cloud(Cloud.CloudType.Level60), new Cloud(Cloud.CloudType.Level70), new Cloud(Cloud.CloudType.Level80), new Cloud(Cloud.CloudType.Level90), new Cloud(Cloud.CloudType.Level10), };
var position = RecursiveBinarySearch<Cloud>(sortedArray, new Cloud(Cloud.CloudType.Level40));
Console.WriteLine($"There is a Level 40 cloud at position {position}.\n");