From 9ec6fddc7caac75d474a78714b92b16188294643 Mon Sep 17 00:00:00 2001
From: Akash Srivastava <71835085+Akashsri3bi@users.noreply.github.com>
Date: Wed, 15 Sep 2021 22:30:13 +0530
Subject: [PATCH] Linear Search Algorithm
Linear Search algorithm
---
LinearSearch/LinearSearch.md | 16 ++++++++++++++++
LinearSearch/LinearSearch.py | 21 +++++++++++++++++++++
2 files changed, 37 insertions(+)
create mode 100644 LinearSearch/LinearSearch.md
create mode 100644 LinearSearch/LinearSearch.py
diff --git a/LinearSearch/LinearSearch.md b/LinearSearch/LinearSearch.md
new file mode 100644
index 0000000..da65a45
--- /dev/null
+++ b/LinearSearch/LinearSearch.md
@@ -0,0 +1,16 @@
+A simple approach is to do a linear search, i.e
+
+Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
+If x matches with an element, return the index.
+If x doesn’t match with any of elements, return -1.
+
+
Time Complexity
+
+The time complexity of the above algorithm is O(n).
+
+Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster-searching comparison to Linear search.
+
+Improve Linear Search Worst-Case Complexity
+
+if element Found at last O(n) to O(1)
+if element Not found O(n) to O(n/2)
\ No newline at end of file
diff --git a/LinearSearch/LinearSearch.py b/LinearSearch/LinearSearch.py
new file mode 100644
index 0000000..65895d4
--- /dev/null
+++ b/LinearSearch/LinearSearch.py
@@ -0,0 +1,21 @@
+# Searching an element in a list/array in python
+# can be simply done using \'in\' operator
+# Example:
+# if x in arr:
+# print arr.index(x)
+
+# If you want to implement Linear Search in python
+
+# Linearly search x in arr[]
+# If x is present then return its location
+# else return -1
+
+def search(arr, x):
+
+ for i in range(len(arr)):
+
+ if arr[i] == x:
+ return i
+
+ return -1
+