-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearch
44 lines (40 loc) · 1.13 KB
/
LinearSearch
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
namespace Algorithms
{
class Program
{
static Boolean LinearSearch(int [] input, int n)
{
foreach (int current in input)
{
if (n == current)
{
return true;
}
}
return false;
}
static int? LinearSearch2(int [] input, int n)
{
foreach (int current in input)
{
if (n == current)
{
return n;
}
}
return null;
}
static void Main(string [] args)
{
int [] arr = {1, 2, 3, 4, 5, 6};
System.Console.WriteLine(LinearSearch(arr, 4));
// Can also be accomplished using the built-in
// function Find() or FindAll(), printing with
// the Array.ForEach() function.
int item = Array.Find(arr, e => e == 3);
System.Console.WriteLine(item);
int[] items = Array.FindAll(arr, e => e >= 5);
Array.ForEach(items, Console.WriteLine);
}
}
}