using System.Diagnostics;
private static int numberToFind = 0;
private static int[] nums = Enumerable.Range(0, 1000000).ToArray();
private static int recursiveBinaryAttempts = 0;
private static int nonRecursiveBinaryAttempts = 0;
private static int oldSchoolAttempts = 0;
public static void Main(string[] args)
Console.WriteLine("Welcome to the 'Find my number' module. 'Like a number in a haystack!' \n/findnumber <number> to start the search!");
commandLine = Console.ReadLine();
command = commandLine.Contains(" ") ? commandLine.Substring(0, commandLine.IndexOf(" ")) : commandLine;
var number = commandLine.Substring(commandLine.IndexOf(' ') + 1);
if (number.Length == -1 || !int.TryParse(number, out n))
Console.WriteLine("Please2 use /findnumber <number> to start your search!");
Console.WriteLine("====================New search!=======================");
Console.WriteLine("Number to find: {0}", n);
Console.WriteLine("Searching in array between:{0} and {1}", nums[0], nums[nums.Length - 1]);
StartRecursiveBinarySearch();
StartNonRecursiveBinarySearch();
StartOldSchoolStyleSearch();
Console.WriteLine("Please use /findnumber <number> to start your search!");
private static void startLambdaSearch()
Console.WriteLine("=======Lambda Style=======");
var stopwatch = new Stopwatch();
bool found = FindNumberLambdaStyle(numberToFind);
Console.WriteLine("Found it after in {0} milliseconds!", stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Not found after in {0} milliseconds!", stopwatch.Elapsed.TotalMilliseconds);
private static void startLinqSearch()
Console.WriteLine("=======Linq Style=======");
var stopwatch = new Stopwatch();
bool found = FindNumberLinqStyle(numberToFind);
Console.WriteLine("Found it after in {0} milliseconds!", stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Not found after in {0} milliseconds!", stopwatch.Elapsed.TotalMilliseconds);
private static bool FindNumberLinqStyle(int numberTofind)
int[] found = (from num in nums
where num == numberTofind
return found.Length == 0 ? false : true;
private static bool FindNumberLambdaStyle(int numberTofind)
int[] found = nums.Where(x => x == numberTofind).ToArray();
return found.Length == 0 ? false : true;
private static void StartRecursiveBinarySearch()
Console.WriteLine("=======Recursive Binary Style=======");
int maxIndex = nums.Length-1;
var stopwatch = new Stopwatch();
bool found = FindNumberRecursivelyInArrayBinaryStyle(numberToFind, leftIndex, maxIndex);
Console.WriteLine("Found it after looping {0} times in {1} milliseconds!", recursiveBinaryAttempts, stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Not found after looping {0} times in {1} milliseconds!", recursiveBinaryAttempts, stopwatch.Elapsed.TotalMilliseconds);
recursiveBinaryAttempts = 0;
private static void StartNonRecursiveBinarySearch()
Console.WriteLine("=======Non recursive Binary Style=======");
var stopwatch = new Stopwatch();
bool found = FindNumberNormallyInArrayBinaryStyle(numberToFind);
Console.WriteLine("Found it after looping {0} times in {1} milliseconds!", nonRecursiveBinaryAttempts, stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Not found after looping {0} times in {1} milliseconds!", nonRecursiveBinaryAttempts, stopwatch.Elapsed.TotalMilliseconds);
nonRecursiveBinaryAttempts = 0;
private static void StartOldSchoolStyleSearch()
Console.WriteLine("=======OldSchool Style=======");
var stopwatch = new Stopwatch();
bool foundOldSchool = FindNumberInArrayOldSchoolStyle(numberToFind);
Console.WriteLine("Found it after looping {0} times in {1} milliseconds!", oldSchoolAttempts, stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Not found after looping {0} times in {1} milliseconds!", oldSchoolAttempts, stopwatch.Elapsed.TotalMilliseconds);
private static bool FindNumberInArrayOldSchoolStyle(int findNumber)
if (currentNumber == findNumber)
private static bool FindNumberNormallyInArrayBinaryStyle(int number)
int maxIndex = nums.Length-1;
while (leftIndex + 1 < maxIndex)
nonRecursiveBinaryAttempts++;
int lookBetween = maxIndex - leftIndex;
int lookTill = lookBetween / 2;
int lookAt = leftIndex + lookTill;
long currentValue = nums[lookAt];
if (currentValue > number)
if (currentValue == number)
private static bool FindNumberRecursivelyInArrayBinaryStyle(int number, int leftIndex, int maxIndex)
if (leftIndex + 1 < maxIndex)
recursiveBinaryAttempts++;
int lookBetween = maxIndex - leftIndex;
int lookTill = lookBetween / 2;
int lookAt = leftIndex + lookTill;
long currentValue = nums[lookAt];
if (currentValue > number)
if (currentValue == number)
return FindNumberRecursivelyInArrayBinaryStyle(number, leftIndex, maxIndex);