Skip to content

Commit ca8fef5

Browse files
committed
add search unsorted array python
1 parent ef5bad3 commit ca8fef5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/Search_Unsorted_Array.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def search(arr, n, x):
2+
3+
#This is the first comparision
4+
if arr[n - 1] == x: return True
5+
6+
#Save last element in back and set last element to target
7+
back = arr[n - 1]
8+
arr[n - 1] = x
9+
10+
i = 0
11+
while i < n:
12+
if arr[i] == x:
13+
arr[n - 1] = back
14+
if i < n - 1: return True
15+
return False
16+
i += 1
17+
18+
print(search([1,4, 5, 2], 4, 2))

0 commit comments

Comments
 (0)