Recursive Insertion Sort | Recursive function to sort an array using insertion sort ; Base case ; Sort first n - 1 elements ; Insert last element at its correct position in sorted array . ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print an array of size n ; Driver Code
< ? php function insertionSortRecursive ( & $ arr , $ n ) { if ( $ n <= 1 ) return ; insertionSortRecursive ( $ arr , $ n - 1 ) ; $ last = $ arr [ $ n - 1 ] ; $ j = $ n - 2 ; while ( $ j >= 0 && $ arr [ $ j ] > $ last ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j -- ; } $ arr [ $ j + 1 ] = $ last ; } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSortRecursive ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >