1. The document describes a program to perform binary search on a 2D matrix where elements are stored in increasing order.
2. It takes the size of the matrix and the element to search as input. The algorithm divides the matrix into half at each step and compares the element to search with the mid element until the element is found or not present.
3. An example run is shown where a 5x5 matrix is input, elements are displayed, a number is searched which is found and its position is printed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
303 views3 pages
Binary Search in N X N Matrix
1. The document describes a program to perform binary search on a 2D matrix where elements are stored in increasing order.
2. It takes the size of the matrix and the element to search as input. The algorithm divides the matrix into half at each step and compares the element to search with the mid element until the element is found or not present.
3. An example run is shown where a 5x5 matrix is input, elements are displayed, a number is searched which is found and its position is printed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3
Ex.
No:3 BINARY SEARCH IN N X N MATRIX
PROBLEM STATEMENT Program to search the given element using an efficient algorithm from the given n x n matrix. AIM To search the given element from n X n matrix using binary search, where the elements of matrix is in increasing order. ALGORITHM 1. Start. 2. Read the sie of the matrix and read the values in increasing order. Procedure for !inary search" #ssign low$%, high$n Read the element to be searched, s &hile 'low(high) !egin *id$'low+high),2 -f 's$$a.i/.mid/) Print 0The element is located at1 -, mid 2lse if 's(a.i/.mid/) 3igh$mid 2lse 4ow$mid+1 2nd 2nd 2nd while 5. 2nd PROGRAM ,, !inary search in a n x n matrix where the elements are in increasing order 6include (iostream.h7 6include (conio.h7 const *$1%8 class matrix 9 1 :rivate" int mat.*/.*/8 :ublic" void create'int n)8 void dis:lay'int n)8 void search'int n,int num)8 ;8 void matrix""create'int n) 9 for 'int i$%8i(n8i++) for 'int <$%8<(n8<++) 9 cout((='=((i+1((=,=((<+1((=) "=8 cin77mat.i/.</8 ; ; void matrix""dis:lay'int n) 9 for 'int i$%8i(n8i++) 9 for 'int <$%8<(n8<++) cout((mat.i/.</((=>t=8 cout((endl((endl8 ; ; void matrix""search'int n,int num) 9 for 'int i$%8i(n8i++) 9 int low$%, high$n?1, mid8 for 'mid$'low+high),28low($high8mid$'low+high),2) 9 if 'mat.i/.mid/$$num) 9 cout((=The number =((num((= is at :osition "=((='=((i+1((=,=((mid+1((=)=8 return8 ; else if 'mat.i/.mid/(num) low$mid+18 else high$mid?18 2 ; ; cout((=The number =((num((= is not :resent in the matrix=8 ; void main') 9 matrix m8 int n,num8 clrscr')8 cout((=2nter order of the matrix @n@"=8 cin77n8 cout((=2nter *atrix elements=((endl8 m.create'n)8 cout((endl((=Aiven *atrix is=((endl((endl8 m.dis:lay'n)8 cout((=>n>n2nter the number to be searched">n>n=8 cin77num8 m.search'n,num)8 getch')8 ; OUTPUT 2nter order of the matrix @n@"5 2nter *atrix elements '1,1) "12 '1,2) "5B '1,5) "5C '2,1) "5D '2,2) "5E '2,5) "BC '5,1) "BE '5,2) "C% '5,5) "C1 Aiven *atrix is 12 5B 5C 5D 5E BC BE C% C1 2nter the number to be searched" BC The number BC is at :osition "'2,5) 3