-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryserach.cs
More file actions
39 lines (29 loc) · 870 Bytes
/
binaryserach.cs
File metadata and controls
39 lines (29 loc) · 870 Bytes
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
using System;
class BinarySerach
{
public static int binarySearch(int [] arr, int l, int r, int f ){
if(r >=1){
int pos = l+(r-l)/2;
if(f == arr[pos]){
return pos;
}
if(arr[pos] > f){
return binarySearch(arr,l, pos-1,f );
}
return binarySearch(arr,pos+1, r, f);
}
return -1;
}
public static void MainC(string[] args)
{
int[] arr = { 2, 3, 4, 10, 40 };
int n = arr.Length;
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
Console.WriteLine("Element not present");
else
Console.WriteLine("Element found at index "
+ result);
}
}