-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearchAlgorithm.cs
More file actions
42 lines (33 loc) · 1.26 KB
/
LinearSearchAlgorithm.cs
File metadata and controls
42 lines (33 loc) · 1.26 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
using System;
namespace LinearSearchAlgorithm
{
public class LinearSearch
{
public int Linear_Search(int[] data, int itemToFind)
{
for (int i = 0; i < data.Length; i++)
{
if (data[i] == itemToFind)
{
return i;
}
}
return -1;
}
static void Main(string[] args)
{
int[] data = { 68, 6, 12, 18, 36, 71 };
LinearSearch result1 = new LinearSearch();
int itemToFind = 36;
int index = result1.Linear_Search(data, itemToFind);
Console.WriteLine((index >= 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found");
itemToFind = 13;
index = result1.Linear_Search(data, itemToFind);
Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found");
itemToFind = 30;
index = result1.Linear_Search(data, itemToFind);
Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found");
Console.ReadLine();
}
}
}