Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created February 12, 2013 15:45
Show Gist options
  • Select an option

  • Save ackintosh/4770795 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/4770795 to your computer and use it in GitHub Desktop.
Selection sort in PHP
<?php
$array = range(1, 20);
shuffle($array);
var_dump($array);
var_dump(selectionsort($array));
function selectionsort($array)
{
$size = count($array);
for ($i = 0; $i < $size; $i++) {
$min_index = $i;
for ($j = $i + 1; $j < $size; $j++) {
if ($array[$j] < $array[$min_index]) $min_index = $j;
}
list($array[$i], $array[$min_index]) = array($array[$min_index], $array[$i]);
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment