Skip to content

Instantly share code, notes, and snippets.

@imorte
Created July 29, 2017 10:11
Show Gist options
  • Select an option

  • Save imorte/3914bccd1c987b55a67639db191e4693 to your computer and use it in GitHub Desktop.

Select an option

Save imorte/3914bccd1c987b55a67639db191e4693 to your computer and use it in GitHub Desktop.
Selection sort php
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . 'vendor/autoload.php';
$array = [5, 1, 0, 8, 1234, 345];
function findSmallest(array $array) {
$value = $array[0];
$index = 0;
foreach ($array as $i => $v) {
if($v < $value) {
$value = $v;
$index = $i;
}
}
return $index;
}
function paste(array $array) {
$newArray = [];
$total = count($array);
for($i = 0; $i < $total; $i++) {
$smallest = findSmallest($array);
array_push($newArray, $array[$smallest]);
array_splice($array, $smallest, 1);
}
return $newArray;
}
dd(paste($array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment