Skip to content

Commit 84a1626

Browse files
ClémentClément
authored andcommitted
Tidy source code.
1 parent c5da545 commit 84a1626

File tree

1 file changed

+51
-32
lines changed
  • source/code/projects/LinearSearch/LinearSearch

1 file changed

+51
-32
lines changed

source/code/projects/LinearSearch/LinearSearch/Program.cs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,59 @@
22

33
class Program
44
{
5-
public static void Main()
5+
public static void Main()
6+
{
7+
// Example value
8+
int[] arrayExample =
69
{
7-
// Example value
8-
int[] arrayExample = { 1, 8, -12, 9, 10, 1, 30, 1, 32, 3 };
9-
10-
bool foundTarget = false;
11-
int target = 8;
12-
13-
for (int i = 0; i < arrayExample.Length; i++)
14-
{
15-
if (arrayExample[i] == target) foundTarget = true;
16-
}
17-
Console.WriteLine(target + " is in the array: " + foundTarget + ".");
18-
19-
20-
// We can optimize this algorithm
21-
// by exiting when the target is found.
10+
1,
11+
8,
12+
-12,
13+
9,
14+
10,
15+
1,
16+
30,
17+
1,
18+
32,
19+
3,
20+
};
21+
22+
bool foundTarget = false;
23+
int target = 8;
24+
25+
for (int i = 0; i < arrayExample.Length; i++)
26+
{
27+
if (arrayExample[i] == target)
28+
foundTarget = true;
29+
}
30+
Console.WriteLine(
31+
target + " is in the array: " + foundTarget + "."
32+
);
2233

23-
bool foundYet = false;
24-
target = 30;
25-
int index = 0;
34+
// We can optimize this algorithm
35+
// by exiting when the target is found.
2636

27-
do
28-
{
29-
if (arrayExample[index] == target) foundYet = true;
30-
index++;
31-
}
32-
while (index < arrayExample.Length && !foundYet);
33-
Console.WriteLine(target + " is in the array: " + foundYet +
34-
"\nNumber of elements inspected: " + (index) + ".");
37+
bool foundYet = false;
38+
target = 30;
39+
int index = 0;
3540

36-
// This would display:
37-
// 30 is in the array: True
38-
// Number of elements inspected: 7.
39-
}
41+
do
42+
{
43+
if (arrayExample[index] == target)
44+
foundYet = true;
45+
index++;
46+
} while (index < arrayExample.Length && !foundYet);
47+
Console.WriteLine(
48+
target
49+
+ " is in the array: "
50+
+ foundYet
51+
+ "\nNumber of elements inspected: "
52+
+ (index)
53+
+ "."
54+
);
55+
56+
// This would display:
57+
// 30 is in the array: True
58+
// Number of elements inspected: 7.
59+
}
4060
}
41-

0 commit comments

Comments
 (0)