You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: projects/linear-search/index.md
+33-25Lines changed: 33 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -2,24 +2,29 @@
2
2
title: Linear Search in Every Language
3
3
layout: default
4
4
date: 2019-10-17
5
-
last-modified: 2020-05-02
5
+
last-modified: 2020-10-15
6
6
featured-image:
7
7
tags: [linear-search]
8
8
authors:
9
9
- frankhart2017
10
+
- the_renegade_coder
10
11
---
11
12
12
13
This article introduces Linear Search which is the simplest search algorithm.
13
14
14
15
## Description
15
16
16
-
Linear search is quite intuitive, it is basically searching an element in an array by traversing the array from the beginning to the end and comparing each item in the array with the key. If a particular array entry matches with the key the position is recorded and the loop is stopped. The algorithm for this is:
17
+
Linear search is quite intuitive, it is basically searching an element in an array by traversing
18
+
the array from the beginning to the end and comparing each item in the array with the key. If a
19
+
particular array entry matches with the key the position is recorded and the loop is stopped.
20
+
The algorithm for this is:
17
21
18
22
1. Define a flag (set it's value to 0) for checking if key is present in array or notation.
19
23
2. Iterate through every element in array.
20
24
3. In each iteration compare the key and the current element.
21
25
4. If they match set the flag to 1, position to the current iteration and break from the loop.
22
-
5. If entire loop is traversed and the element is not found the value of flag will be 0 and user can notified that key is not in array.
26
+
5. If entire loop is traversed and the element is not found the value of flag will be 0 and user
27
+
can notified that key is not in array.
23
28
24
29
### Performance
25
30
@@ -43,7 +48,7 @@ Linear search is not efficient for large arrays, but for relatively smaller arra
43
48
<br>key != array[i]
44
49
45
50
<b>Iteration 2</b>
46
-
<br>array[i] = array[i] = 2
51
+
<br>array[i] = array[1] = 2
47
52
<br>key = 3
48
53
<br>key != array[i]
49
54
@@ -57,38 +62,41 @@ Linear search is not efficient for large arrays, but for relatively smaller arra
57
62
58
63
## Requirements
59
64
60
-
Write a sample program that takes a key in the format 3 and a list of numbers in the format "1, 2, 3, 4, 5".
65
+
Write a sample program that takes a list of numbers (e.g. "1, 2, 3, 4, 5") and a key (e.g. "3").
61
66
62
-
### C++ Program
63
-
64
-
```console
65
-
$ cd archives/c/c-plus-plus/
66
-
$ g++ -o linear-search linear-search.cpp
67
-
$ ./linear-search 3 "1, 2, 3, 4, 5"
68
-
3 found at position 2.
69
67
```
70
-
71
-
### Python Program
72
-
73
-
```console
74
-
$ cd archives/p/python/
75
-
$ python linear-search.py 6 "1, 2, 3, 4, 5"
76
-
6 not found in the array .
68
+
linear-search.lang "1, 4, 2, 9" "3"
77
69
```
78
70
79
71
In addition, there should be some error handling for situations where the user
0 commit comments