Skip to content

Commit c0b2134

Browse files
committed
linear search
1 parent c42992a commit c0b2134

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Diff for: Practice Concepts/Search/linear-search.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Pythonn implementation of Linear Search Algorithm
2+
3+
def linearSearch(array, x):
4+
for i in range(len(array)):
5+
if(array[i] == x):
6+
return i
7+
return -1
8+
9+
# Driver Code
10+
array = [2, 4, 0, 1, 9]
11+
x = 1
12+
result = linearSearch(array, x)
13+
if(result == -1):
14+
print("Element not found")
15+
else:
16+
print("Element found at index: ", result)

Diff for: README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,7 @@
449449
<a href="#counting-sort">Counting Sort</a> •
450450
<a href="#radix-sort">Radix Sort</a> •
451451
<a href="#bucket-sort">Bucket Sort</a> •
452-
<a href="#heap-sort">Heap Sort</a> •
453-
<a href="#shell-sort">Shell Sort</a>
452+
<a href="#heap-sort">Heap Sort</a>
454453
</p>
455454

456455
### [Bubble Sort](/Practice%20Concepts/Sorting/bubble-sort.py)
@@ -583,6 +582,14 @@
583582
<a href="#binary-search">Binary Search</a>
584583
</p>
585584

585+
### [Linear Search](/Practice%20Concepts/Search/linear-search.py)
586+
* Sequential searching algorithm where we start from one end and check every element of the list until the desired element is found.
587+
* Method:
588+
- Start from the first element and compare it with each element in the array
589+
- Return the index if there is a match
590+
591+
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/linear.gif)
592+
586593
## Greedy Algorithms
587594
<p align="center">
588595
<a href="#introduction-to-greedy-algorithms">Introduction to Greedy Algorithms</a> •

Diff for: Resources/Animations/linear.gif

11.7 KB
Loading

0 commit comments

Comments
 (0)