Saddleback Search Algorithm in a 2D array | PHP program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code
< ? php $ MAX = 100 ; function search ( $ mat , $ m , $ n , $ x ) { $ i = $ m - 1 ; $ j = 0 ; while ( $ i >= 0 && $ j < $ n ) { if ( $ mat [ $ i ] [ $ j ] == $ x ) return true ; if ( $ mat [ $ i ] [ $ j ] > $ x ) $ i -- ; else $ j ++ ; } return false ; } $ mat = array ( array ( 10 , 20 , 30 , 40 ) , array ( 15 , 25 , 35 , 45 ) , array ( 27 , 29 , 37 , 48 ) , array ( 32 , 33 , 39 , 50 ) , array ( 50 , 60 , 70 , 80 ) ) ; if ( search ( $ mat , 5 , 4 , 29 ) ) echo " Yes " ; else echo " No " ; ? >