Insertion sort php


<?php
/**
 * An unsorted list of integers
 * Worst case O(n^2)
 * Best case O(n)
 * @param array $unsorted
 */
function insertionSort(array &$unsorted){
    for($i = 1; $i < count($unsorted); $i++){ 
        $temp = $unsorted[$i]; $j = $i - 1; 
        while ( ($j >= 0) &&  ($unsorted[$j] < $temp)){
            $unsorted[$j + 1] = $unsorted[$j];
            $unsorted[$j--] = $temp;
        }
    }
}
This entry was posted in algorithms, php. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *