Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d27968b

Browse files
Ishani08poyea
authored andcommitted
Create Searching in sorted matrix (TheAlgorithms#738)
* Create Searching in sorted matrix * Rename Searching in sorted matrix to searching_in_sorted_matrix.py
1 parent 96c36f8 commit d27968b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎matrix/searching_in_sorted_matrix.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def search_in_a_sorted_matrix(mat, m, n, key):
2+
i, j = m - 1, 0
3+
while i >= 0 and j < n:
4+
if key == mat[i][j]:
5+
print('Key %s found at row- %s column- %s' % (key, i + 1, j + 1))
6+
return
7+
if key < mat[i][j]:
8+
i -= 1
9+
else:
10+
j += 1
11+
print('Key %s not found' % (key))
12+
13+
14+
def main():
15+
mat = [
16+
[2, 5, 7],
17+
[4, 8, 13],
18+
[9, 11, 15],
19+
[12, 17, 20]
20+
]
21+
x = int(input("Enter the element to be searched:"))
22+
print(mat)
23+
search_in_a_sorted_matrix(mat, len(mat), len(mat[0]), x)
24+
25+
26+
if __name__ == '__main__':
27+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /