Optimizing a binary search algorithm I have implemented a binary search algorithm in C#. Can anyone give me suggestions on improving its effectiveness?

using System;

namespace bs2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] arr = new int[n];

            for(int i=0;i&lt;n;i++)
            {
                arr[i] = i*14; //14 is a random number, I multiply by a number so that arr[i] would not be equal to i.
            }

            int searchingfor = int.Parse(Console.ReadLine());
            int min = 0;int max = n-1;int mid = (n-1)/2;
            while (true)
            {
                if (arr[mid] == searchingfor)
                {
                    Console.WriteLine("Element is on position {0}", mid);
                    break;
                }else if (arr[mid] &lt; searchingfor)
                {
                    min = mid;
                    mid = (min + max) / 2;
                }else if (arr[mid] &gt; searchingfor)
                {
                    max = mid;
                    mid = (min + max) / 2;
                }
            }

        }
    }
}
The core of your search algorithm is OK except for one thing:

The while(true) loop will run forever, if the searchingFor is not in the array.

In addition you should organize your code properly by separating functionality:  test input/output in one function and the algorithm in another: 

void TestBinarySearch()
{
  Console.Write("Enter Array Length: ");
  int length = int.Parse(Console.ReadLine());
  Console.Write("Enter Search Target: ");
  int target = int.Parse(Console.ReadLine());

  Random rand = new Random(5);
  int[] data = Enumerable.Range(0, length).Select(n =&gt; rand.Next(length)).OrderBy(d =&gt; d).ToArray();
  int foundIndex = BinarySearch(data, target);
  if (foundIndex &gt;= 0)
    Console.WriteLine($"Target found at: {foundIndex}");
  else
    Console.WriteLine("Target not found");
}

int BinarySearch(int[] data, int target)
{
  int min = 0;
  int max = data.Length - 1;
  int mid = (data.Length - 1) / 2;

  while (min != mid &amp;&amp; max != mid)
  {
    if (data[mid] == target)
    {
      return mid;
    }

    if (data[mid] &lt; target)
    {
      min = mid;
      mid = (min + max) / 2;
    }
    else if (data[mid] &gt; target)
    {
      max = mid;
      mid = (min + max) / 2;
    }
  }

  return -1;
}