Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

insertion sort in php

function insertion_sort($arr){
    $size = count($arr);
    for ($i = 1; $i < $size; $i++) {
        $key = $arr[$i];
        $j = $i-1;
        while ($j >= 0 && $arr[$j] > $key) {
            $arr[$j + 1] = $arr[$j];
            $j = $j - 1;
        }
        $arr[$j + 1] = $key;
    }
}
 
PREVIOUS NEXT
Tagged: #insertion #sort #php
ADD COMMENT
Topic
Name
2+3 =