-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Linear_Search.cs
45 lines (37 loc) · 993 Bytes
/
Linear_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
using System;
namespace Linear_Search
{
class Program
{
public static int LinearSearch(int[] array, int size, int desired)
{
for (int i = 0; i < size; i++)
{
// Return position if element is found
if (array[i] == desired)
return i;
}
return -1;
}
// Driver Function
public static void Main(String[] args)
{
int[] array = {2, 4, 6, 7, 3, 1, 5};
// Element 4 to be searched
if(LinearSearch(array, 7, 4) != -1)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
// Element 9 to be searched
if(LinearSearch(array, 7, 9) != -1)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
Console.ReadLine();
}
}
}
/* Output
Found
Not Found
*/