Skip to content

Commit cb7da85

Browse files
committed
Matrix search code added for a sorted matrix
1 parent 8aba976 commit cb7da85

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sorting_Searching/matrix_in_search.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def search(mat, n, x):
2+
3+
i = 0
4+
j = n - 1
5+
while (i < n and j >= 0):
6+
7+
if (mat[i][j] == x):
8+
9+
print("Element found at row: ", i, ", column: ", j)
10+
return 1
11+
12+
if (mat[i][j] > x):
13+
j -= 1
14+
15+
# if mat[i][j] < x
16+
else:
17+
i += 1
18+
19+
print("Element not found")
20+
return 0
21+
22+
if __name__ == "__main__":
23+
mat = [[10, 20, 30, 40],
24+
[15, 25, 35, 45],
25+
[27, 29, 37, 48],
26+
[32, 33, 39, 50]]
27+
28+
# Function call
29+
search(mat, 4, 29)

0 commit comments

Comments
 (0)