Find the minimum element in a sorted and rotated array | PHP program to find minimum element in a sorted and rotated array ; This condition is needed to handle the case when array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like ( 3 , 4 , 5 , 1 , 2 ) ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver Code
< ? php function findMin ( $ arr , $ low , $ high ) { if ( $ high < $ low ) return $ arr [ 0 ] ; if ( $ high == $ low ) return $ arr [ $ low ] ; $ mid = $ low + ( $ high - $ low ) / 2 ; if ( $ mid < $ high && $ arr [ $ mid + 1 ] < $ arr [ $ mid ] ) return $ arr [ $ mid + 1 ] ; if ( $ mid > $ low && $ arr [ $ mid ] < $ arr [ $ mid - 1 ] ) return $ arr [ $ mid ] ; if ( $ arr [ $ high ] > $ arr [ $ mid ] ) return findMin ( $ arr , $ low , $ mid - 1 ) ; return findMin ( $ arr , $ mid + 1 , $ high ) ; } $ arr1 = array ( 5 , 6 , 1 , 2 , 3 , 4 ) ; $ n1 = sizeof ( $ arr1 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr1 , 0 , $ n1 - 1 ) . " STRNEWLINE " ; $ arr2 = array ( 1 , 2 , 3 , 4 ) ; $ n2 = sizeof ( $ arr2 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr2 , 0 , $ n2 - 1 ) . " STRNEWLINE " ; $ arr3 = array ( 1 ) ; $ n3 = sizeof ( $ arr3 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr3 , 0 , $ n3 - 1 ) . " STRNEWLINE " ; $ arr4 = array ( 1 , 2 ) ; $ n4 = sizeof ( $ arr4 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr4 , 0 , $ n4 - 1 ) . " STRNEWLINE " ; $ arr5 = array ( 2 , 1 ) ; $ n5 = sizeof ( $ arr5 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr5 , 0 , $ n5 - 1 ) . " STRNEWLINE " ; $ arr6 = array ( 5 , 6 , 7 , 1 , 2 , 3 , 4 ) ; $ n6 = sizeof ( $ arr6 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr6 , 0 , $ n6 - 1 ) . " STRNEWLINE " ; $ arr7 = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n7 = sizeof ( $ arr7 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr7 , 0 , $ n7 - 1 ) . " STRNEWLINE " ; $ arr8 = array ( 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 ) ; $ n8 = sizeof ( $ arr8 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr8 , 0 , $ n8 - 1 ) . " STRNEWLINE " ; $ arr9 = array ( 3 , 4 , 5 , 1 , 2 ) ; $ n9 = sizeof ( $ arr9 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr9 , 0 , $ n9 - 1 ) . " STRNEWLINE " ; ? >