FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

inserting a 2d matrix search algo in searches folder under algorithms by HimadriPathak · Pull Request #133 · AllAlgorithms/python · GitHub

This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/ python Public archive
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions algorithms/searches/2D_array_search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#it's a searching algorithm for sorted 2D matrix and complexity O(n+m) where n is number of rows and m is number of columns
n,m= map(int,input().split()) #taking row and column input
arr = [[int(input()) for x in range (m)] for y in range(n)] #entering elements into the matrix

k = int(input("enter the search element: "))
flag = False

i,j = 0, m-1
#main algorithm
while((i>=0) and (j>=0) and (i<n) and (j<m)):
if(arr[i][j] == k):
flag = True
break
elif(arr[i][j]> k):
j-=1
else:
i+=1

if(flag):
print("element found")
else:
print("element not found")

Back | FazBrowse Home | New Git URL