Insertion and selection sort in PHP I've implemented InsertionSort and SelectionSort in PHP and tested it with an array of 20.000 unique integers.

It took many seconds to complete the insertion selection sorts.  For comparison, I saw a Java implementation that only took 3 seconds for both combined. I wanted to ask if my implementations are just bad or if this is a hardware/language based problem.

Here are my methods:

$rand = randomArray(20000);
insertionSort($rand);
selectionSort($rand);


function insertionSort($sort_array){
    $time_start = microtime(true);
    for ($i = 0; $i &lt; count($sort_array)-1; $i++){
      for ($j = $i + 1; $j &gt; 0; $j--)
        {    
          if($sort_array[$j-1] &gt; $sort_array[$j]){
            $temp = $sort_array[$j-1];
            $sort_array[$j-1] = $sort_array[$j];
            $sort_array[$j] = $temp;               
          }         
        } 
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo $time;
    return $sort_array;
}

function selectionSort($sort_array) 
{
    $time_start = microtime(true);
    for ($i = 0; $i &lt; count($sort_array) - 1; $i++)
    {
        $min = $i;  
        for ($j = $i + 1; $j &lt; count($sort_array); $j++)
        {
            if ($sort_array[$j] &lt; $sort_array[$min]){
              $min = $j;
        } 
      $temp = $sort_array[$i];
      $sort_array[$i] = $sort_array[$min];
      $sort_array[$min] = $temp;
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo $time;
    return $sort_array;
    }
} 

function randomArray($max){
    $random = range(0, $max-1);
    shuffle($random );
    return  $random;
}
One possible inefficiency is that you are copying the array when you call the function.  If you pass parameters by reference, then you could avoid allocating and copying the array.  See the difference in these two functions that just perform one swap:

function f($a) {
    $swap = $a[0];
    $a[0] = $a[1];
    $a[1] = $swap;
    return $a;
}

function g(&amp;$a) {
    $swap = $a[0];
    $a[0] = $a[1];
    $a[1] = $swap;
}

$xyz = array('x', 'y', 'z');
print_r($xyz);                  # array { [0] =&gt; x  [1] =&gt; y  [2] =&gt; z }
$yxz = f($xyz);
print_r($xyz);                  # array { [0] =&gt; x  [1] =&gt; y  [2] =&gt; z }
print_r($yxz);                  # array { [0] =&gt; y  [1] =&gt; x  [2] =&gt; z }
g($xyz);
print_r($xyz);                  # array { [0] =&gt; y  [1] =&gt; x  [2] =&gt; z }




That aside, there is not that much you can do to optimize your two sorting algorithms.  You could try obtaining the length count($sort_array) just once, before the loop, to see if makes any difference.

The important thing to note, though, is that both insertion sort and selection sort are O(n2) algorithms.  That means that if you double the size of the array, it will take approximately quadruple the amount of time to complete the sort.  If you care about performance when sorting large lists, you don't use insertion sort or selection sort.  You pick a more efficient algorithm that tends to work in O(n log n) time, such as quicksort.

Comparing performance across languages is not a very useful way to judge the quality of your code.  To make a comparison that is at least somewhat fair, try racing against PHP's own array_sort() function.