Jump Search | PHP program to implement Jump Search ; Finding block size to be jumped ; Finding the block where element is present ( if it is present ) ; Doing a linear search for x in block beginning with prev . ; If we reached next block or end of array , element is not present . ; If element is found ; Driver program to test function ; Find the index of ' $ x ' using Jump Search ; Print the index where ' $ x ' is located
< ? php function jumpSearch ( $ arr , $ x , $ n ) { $ step = sqrt ( $ n ) ; $ prev = 0 ; while ( $ arr [ min ( $ step , $ n ) - 1 ] < $ x ) { $ prev = $ step ; $ step += sqrt ( $ n ) ; if ( $ prev >= $ n ) return -1 ; } while ( $ arr [ $ prev ] < $ x ) { $ prev ++ ; if ( $ prev == min ( $ step , $ n ) ) return -1 ; } if ( $ arr [ $ prev ] == $ x ) return $ prev ; return -1 ; } $ arr = array ( 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 ) ; $ x = 55 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ index = jumpSearch ( $ arr , $ x , $ n ) ; echo " Number ▁ " . $ x . " ▁ is ▁ at ▁ index ▁ " . $ index ; return 0 ; ? >