Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions LinearSearch/LinearSearch.md
Original file line number Diff line number Diff line change
@@ -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.

<h1>Time Complexity</h1>

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

<h2>if element Found at last O(n) to O(1)</h2>
<h2>if element Not found O(n) to O(n/2)</h2>
21 changes: 21 additions & 0 deletions LinearSearch/LinearSearch.py
Original file line number Diff line number Diff line change
@@ -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