mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Create Searching in sorted matrix (#738)
* Create Searching in sorted matrix * Rename Searching in sorted matrix to searching_in_sorted_matrix.py
This commit is contained in:
parent
96c36f8286
commit
d27968b78d
27
matrix/searching_in_sorted_matrix.py
Normal file
27
matrix/searching_in_sorted_matrix.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
def search_in_a_sorted_matrix(mat, m, n, key):
|
||||||
|
i, j = m - 1, 0
|
||||||
|
while i >= 0 and j < n:
|
||||||
|
if key == mat[i][j]:
|
||||||
|
print('Key %s found at row- %s column- %s' % (key, i + 1, j + 1))
|
||||||
|
return
|
||||||
|
if key < mat[i][j]:
|
||||||
|
i -= 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
print('Key %s not found' % (key))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
mat = [
|
||||||
|
[2, 5, 7],
|
||||||
|
[4, 8, 13],
|
||||||
|
[9, 11, 15],
|
||||||
|
[12, 17, 20]
|
||||||
|
]
|
||||||
|
x = int(input("Enter the element to be searched:"))
|
||||||
|
print(mat)
|
||||||
|
search_in_a_sorted_matrix(mat, len(mat), len(mat[0]), x)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user