using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Example
{
static void Main()
{
Console.WriteLine("Enter Array Size");
int n = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(string.Format("Enter {0} Array Elements Separated by space", n));
int[] ArrayElements = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), arrTemp => int.Parse(arrTemp));
Console.WriteLine("Enter value whose index we want");
int PValue = Convert.ToInt16(Console.ReadLine());
int low = 0;
int high = n - 1;
int mid = -1;
int Answer = -1;
while (low <= high)
{
mid = (low + high) / 2;
if (ArrayElements[mid] == PValue)
{
Answer = mid;
break;
}
else if (ArrayElements[mid] < PValue)
{
low = mid + 1;
}
else if (ArrayElements[mid] > PValue)
{
high = mid - 1;
}
}
if (Answer == -1)
{
Console.WriteLine("No Such Element Found in Array");
}
else
{
Console.WriteLine(string.Format("Index of value {0} is {1}", PValue, Answer));
}
Console.Read();
}
}
}
Output 1 :
Time Complexity : 0 (log2(n)) = 0(log(n))