Skip to content

Commit c5da545

Browse files
ClémentClément
authored andcommitted
Improving notes on linear search algorithm.
1 parent fe474f7 commit c5da545

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
class Program
4+
{
5+
public static void Main()
6+
{
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.
22+
23+
bool foundYet = false;
24+
target = 30;
25+
int index = 0;
26+
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) + ".");
35+
36+
// This would display:
37+
// 30 is in the array: True
38+
// Number of elements inspected: 7.
39+
}
40+
}
41+

source/docs/programming_and_computer_usage/complexity.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,23 @@ The reason why worst case is generally preferred is because:
159159

160160
### Linear search algorithm
161161

162-
The [linear search algorithm](https://princomp.github.io/lectures/data/search#finding-a-particular-value) look for a particular value in an array. The version that exit exit prematurely the loop when the target value is found has the following complexity:
162+
The [linear search algorithm](https://princomp.github.io/lectures/data/search#finding-a-particular-value) look for a particular value in an array by inspecting the values one after the other:
163+
164+
```{download="./code/projects/LinearSearch.zip"}
165+
!include`snippetStart="// Example value",snippetEnd="// We can optimize this algorithm"` code/projects/LinearSearch/LinearSearch/Program.cs
166+
```
167+
168+
169+
It can be slightly tuned, to exit as soon as the target value is found:
170+
171+
```{download="./code/projects/LinearSearch.zip"}
172+
!include`snippetStart="// by exiting when the target is found.",snippetEnd="// This would display:"` code/projects/LinearSearch/LinearSearch/Program.cs
173+
```
174+
175+
The version that exit exit prematurely the loop when the target value is found has the following complexity (in terms of "number of steps"):
163176

164177
- The **best case** is if the target is the very first value, in this case, the time complexity is $O(c)$.
165-
- The **worst case** is if the target is the very last value, in this case the time complexity is $O(n)$ where $n$ is the size of the array.
178+
- The **worst case** is if the target is the very last value (or is not in the array), in this case the time complexity is $O(n)$ where $n$ is the size of the array.
166179
- The **average case** is $O(n)$.
167180

168181
Note that the space usage of this algorithm is $O(c)$, it requires only one variable if we do not copy the array.

source/lectures/data/search.md

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,16 @@ The simplest way to perform such a search is to
3636
#. Set the Boolean variable to `false`,
3737
#. Inspect the values in `arrayExample` one by one, comparing them to `target`, and setting the Boolean variable to `true` if they are identical.
3838

39-
```
40-
int[] arrayExample = { 1, 8, -12, 9, 10, 1, 30, 1, 32, 3 };
4139

42-
bool foundTarget = false;
43-
int target = 8;
44-
45-
for (int i = 0; i < arrayExample.Length; i++)
46-
{
47-
if (arrayExample[i] == target) foundTarget = true;
48-
}
49-
Console.WriteLine(target + " is in the array: " + foundTarget + ".");
40+
```{download="./code/projects/LinearSearch.zip"}
41+
!include`snippetStart="// Example value",snippetEnd="// We can optimize this algorithm"` code/projects/LinearSearch/LinearSearch/Program.cs
5042
```
5143

5244
Note that in the particular example above, we could have stopped exploring the array after the second index, since the target value was found.
5345
A slightly different logic would allow to exit prematurely the loop when the `target` value is found:
5446

55-
```
56-
int[] arrayExample = { 1, 8, -12, 9, 10, 1, 30, 1, 32, 3 };
57-
58-
bool foundYet = false;
59-
int target = 30;
60-
int index = 0;
61-
62-
do
63-
{
64-
if (arrayExample[index] == target) foundYet = true;
65-
index++;
66-
}
67-
while (index < arrayExample.Length && !foundYet);
68-
Console.WriteLine(target + " is in the array: " + foundYet +
69-
"\nNumber of elements inspected: " + (index) +".");
47+
```{download="./code/projects/LinearSearch.zip"}
48+
!include`snippetStart="// by exiting when the target is found.",snippetEnd="// This would display:"` code/projects/LinearSearch/LinearSearch/Program.cs
7049
```
7150

7251
This code would display:

0 commit comments

Comments
 (0)