-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path08 ● Binary Search.cs
70 lines (68 loc) · 1.84 KB
/
08 ● Binary Search.cs
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
70
using System;
namespace Binary_Search
{
class Program
{
public void Sort(int[] arr, int size, int find)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[j] > arr[i])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public void bs1(int size, int[] arr, int find)
{
int s = 0, e = size - 1;
bool h = false;
while (s <= e)
{
int mid = (s + e) / 2;
if (arr[mid] == find)
{
h = true;
break;
}
else if (arr[mid] > find)
{
e = mid - 1;
}
else
{
s = mid + 1;
}
}
if (h)
{
Console.WriteLine("Found");
}
else
{
Console.WriteLine("Not Found");
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter the size of array : ");
int x = int.Parse(Console.ReadLine());
int[] arr = new int[x + 10];
for (int i = 0; i < x; i++)
{
Console.WriteLine("Enter the {0} element : ", i);
arr[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Enter the number to find : ");
int k = int.Parse(Console.ReadLine());
Program S = new Program();
S.Sort(arr, x, k);
S.bs1(x, arr, k);//Found Bool
}
}
}