Reallocation of elements based on Locality of Reference | A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code
< ? php function search ( $ arr , $ n , $ x ) { $ res = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ x == $ arr [ $ i ] ) $ res = $ i ; if ( $ res == -1 ) return false ; $ temp = $ arr [ $ res ] ; for ( $ i = $ res ; $ i > 0 ; $ i -- ) $ arr [ $ i ] = $ arr [ $ i - 1 ] ; $ arr [ 0 ] = $ temp ; return true ; } $ arr = array ( 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 ) ; $ q = array ( 63 , 63 , 86 , 63 , 78 ) ; $ n = sizeof ( $ arr ) ; $ m = sizeof ( $ q ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( search ( $ arr , $ n , $ q [ $ i ] ) ) echo " Yes ▁ " ; else echo " No ▁ " ;