-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cs
More file actions
53 lines (49 loc) · 1.32 KB
/
LinearSearch.cs
File metadata and controls
53 lines (49 loc) · 1.32 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
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataStructureSamples
{
public class Search_Linear
{
public static void Main()
{
print();
}
public static void print()
{
int i, no;
Console.WriteLine("Enter No : ");
no = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[no];
for (i = 0; i < arr.Length; i++)
{
Console.WriteLine("Enter Data {0} : ", (i + 1));
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter Data to Search :");
int data = Convert.ToInt32(Console.ReadLine());
int res = Search(arr, data);
if (res != -1)
{
Console.WriteLine("Found at {0} Position ", res);
}
else
{
Console.WriteLine("Not Found");
}
}
public static int Search(int[] arr, int target)
{
for (int i = 0; i < arr.Length; i++)
{
if (target == arr[i])
{
return (i + 1);
}
}
return -1;
}
}
}