by Devansh

Posted on 23 Oct 2018 12:10:22 (5 months ago)

**Selection sort** is a **sorting algorithm**. This **sorting algorithm** is an in place comparison based **algorithm** in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.

```
function selectionSort($arr) {
$len = count($arr);
for ($i=0; $i<$len; $i++) {
$min = $i;
for ($j=$i+1; $j<$len; $j++) {
if ($arr[$j] < $arr[$min]) {
$min = $j;
}
}
if ($min != $i) {
$tmp = $arr[$i];
$arr[$i] = $arr[$min];
$arr[$min] = $tmp;
}
}
return $arr;
}
$arr = [10, 20, 5, 11, 6, 25, 1, 9, 21];
print_r(selectionSort($arr));
```