A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Selection Sort in Php page! Here, you'll find the source code for this program as well as a description of how the program works.
<?php
if (empty($argv[1])) {
exit('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"');
}
$numbers = array_map('intval', explode(',', $argv[1]));
$array_size = count($numbers);
if ($array_size <= 1) {
exit('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"');
}
function selection_sort($data)
{
for ($i = 0; $i < count($data) - 1; $i++) {
$min = $i;
for ($j = $i + 1; $j < count($data); $j++) {
if ($data[$j] < $data[$min]) {
$min = $j;
}
}
$data = swap_positions($data, $i, $min);
}
return $data;
}
function swap_positions($data1, $left, $right)
{
$backup = $data1[$right];
$data1[$right] = $data1[$left];
$data1[$left] = $backup;
return $data1;
}
echo implode(', ', selection_sort($numbers));
Selection Sort in Php was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.