-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (68 loc) · 1.67 KB
/
Program.cs
File metadata and controls
74 lines (68 loc) · 1.67 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace linearSearch
{
class Program
{
public static int LinearSearch(ref int[] x, int valueToFind)
{
for(int i = 0; i < x.Length; i++){
if(valueToFind == x[i]){
return i;
}
}
return -1;
}
public static void DisplayElements(ref int[] xArray, char status, string sortname)
{
if(status == 'a')
{
Console.WriteLine("After algorithm usage: "+ sortname);
}else{
Console.WriteLine("Before sorting.");
}
for(int i = 0; i <= xArray.Length - 1; i++)
{
if((i != 0) && (i % 10 == 0))
{
Console.Write("\n");
}
Console.Write(xArray[i] + " ");
}
Console.ReadLine();
}
static void MixDataUp(ref int[] x, Random rdn)
{
for(int i = 0; i <= x.Length - 1; i++)
{
x[i] = (int)(rdn.NextDouble() * x.Length);
}
}
static void Main(string[] args)
{
const int nItems = 50;
Random rdn = new Random(nItems);
int[] xdata = new int[nItems];
MixDataUp(ref xdata, rdn);
DisplayElements(ref xdata, 'b', "");
Console.WriteLine("Linear search algorithm: ");
int location = LinearSearch(ref xdata, xdata[5]);
if(location == -1)
{
Console.WriteLine("Value not found.");
}else{
Console.WriteLine("Location found = {0}", location);
}
location = LinearSearch(ref xdata, 17);
if(location == -1){
Console.WriteLine("Value not found.");
}else{
Console.WriteLine("Value found {0}", location);
}
Console.WriteLine("\n\n");
}
}
}