-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort
More file actions
32 lines (27 loc) · 764 Bytes
/
Selection_Sort
File metadata and controls
32 lines (27 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
Selection Sort written in PHP
$list = [1,2,3,3,44,5,6,2,3,2,33,8,8,6,"Kanyin",9,24,21,3,44,5,102,33,43,"James", "Johnson", "Joe", "John", "Jacob", "Josh","Jasmine"];
selectionSort( $list );
Live demo : https://3v4l.org/5s3nW
*/
function selectionSort( array $lst )
{
$numList = count( $lst );
for( $i=0; $i < $numList; $i++ )
{
$index = 0;
$smallest = $lst[$i];
for( $j = $i; $j < $numList; $j++ )
{
if( $lst[$j] < $smallest )
{
$smallest = $lst[$j];
$index = $j;
}
$temp = $lst[$i];
$lst[$i] = $smallest;
$lst[$index] = $temp;
}
echo ( implode(" ", $lst));
}
}