-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBinary Search Implementation.cs
More file actions
69 lines (62 loc) · 2.04 KB
/
Copy pathBinary Search Implementation.cs
File metadata and controls
69 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Binary_Search
{
class Program
{
static void Main(string[] args)
{
int i, j, key, position, N,lb,ub,mid;
Console.WriteLine("Binary Search Implementation");
Console.WriteLine("\n");
Console.WriteLine("Enter the N Elements of the Array");
N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
int[] a = new int[N];
Console.WriteLine("Enter the input Values For the Array:");
for (i = 0; i < N; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n");
Console.WriteLine("The Input array Contains Following Elements:");
for (i = 0; i < N; i++)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine("\n");
lb = 0;
ub = N - 1;
Console.WriteLine("enter the key Elements to be searched");
key = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("The Search Successful:");
for (j = 0; j < N; j++)
{
while (ub >= lb)
{
mid = (ub + lb) / 2;
if (key < a[mid])
{
ub = mid - 1;
}
else if (key > a[mid])
{
lb = mid + 1;
}
else if (key == a[mid])
{
position = mid;
Console.Write(position);
lb = N;
break;
}
}
}
Console.ReadLine();
}
}
}