Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Created November 30, 2015 03:50
Show Gist options
  • Select an option

  • Save KerryJones/96a0b49df9c10040353d to your computer and use it in GitHub Desktop.

Select an option

Save KerryJones/96a0b49df9c10040353d to your computer and use it in GitHub Desktop.
PHP Selection Sort O(n^2)
<?php
// O(n^2)
function selectionSort( array $array ) {
$length = count($array);
for ( $i = 0; $i < $length; $i++) {
$min = $i;
for( $j = $i + 1; $j < $length; $j++ ) {
if ( $array[$min] > $array[$j] )
$min = $j;
}
$temp = $array[$i];
$array[$i] = $array[$min];
$array[$min] = $temp;
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment