Selection Sort Algorithm in PHP
Selection sort algorithm was developed using comparison algorithm.
This algorithm picks an element from the left-hand side and finds the smallest element on the right-hand side,
finally it swaps the smallest element to its position and it continues the process for each element with direction left to right except the last element.
But it's not an efficient on medium or large data sets.
Time Complexity
- Best complexity: n^2
- Average complexity: n^2
- Worst complexity: n^2
Selection Sort Implementation in PHP
PHP Input Screen
<?php
class SelectionSortExample {
public function selectionSort(&$arr) {
$length = sizeof($arr);
for($i = 0; $i < $length - 1; $i++) {
$minIndex = $i;
for($j = $i + 1; $j < $length; $j++) {
if($arr[$minIndex] > $arr[$j]) {
$minIndex = $j;
}
}
$temp = $arr[$minIndex];
$arr[$minIndex] = $arr[$i];
$arr[$i] = $temp;
}
}
public function printArray(&$arr) {
for($i = 0; $i < sizeof($arr); $i++) {
echo($arr[$i] . " ");
}
echo("\n");
}
}
$obj = new SelectionSortExample();
$arr = array();
array_push($arr, 15, 3, 12, 6, -9, 9, 0);
echo("Before Sorting: ");
$obj->printArray($arr);
$obj->selectionSort($arr, 0, sizeof($arr) - 1);
echo("After Sorting: ");
$obj->printArray($arr);
PHP Output Screen
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
Selection sort implementation steps:
Let's take the input array as { 15, 3, 12, 6, -9, 9, 0 } and we can see how the Selection sort is sorting the array.
- Select an index of an element from the left hand-side (i.e. initially an index 0)
- Search an index of the smallest element comparing with all other right-hand side elements with the value of the currently selected index
- Swap the value of the smallest element to the currently selected index
- Apply step 1, 2 and 3, by picking the rest of the element except the last element of an array by incrementing the index value by 1.
Selection Sort Implementation in PHP with explanation
PHP Input Screen
<?php
class SelectionSortExample {
public function selectionSort(&$arr) {
// Get the length of an array
$length = sizeof($arr);
// Loop all the element in an array except the last element
for($i = 0; $i < $length - 1; $i++) {
$minIndex = $i;
// Find an index of a lowest element in the right-hand side
// corresponding to the element arr[i].
for($j = $i + 1; $j < $length; $j++) {
if($arr[$minIndex] > $arr[$j]) {
$minIndex = $j;
}
}
// Swap the lowest element with the current element (i.e. arr[i] <=> arr[minIndex])
$temp = $arr[$minIndex];
$arr[$minIndex] = $arr[$i];
$arr[$i] = $temp;
}
}
public function printArray(&$arr) {
// Loop the item and print each item in the console with white space seperated
for($i = 0; $i < sizeof($arr); $i++) {
echo($arr[$i] . " ");
}
echo("\n");
}
}
$obj = new SelectionSortExample();
$arr = array();
array_push($arr, 15, 3, 12, 6, -9, 9, 0);
echo("Before Sorting: ");
$obj->printArray($arr);
$obj->selectionSort($arr, 0, sizeof($arr) - 1);
echo("After Sorting: ");
$obj->printArray($arr);
PHP Output Screen
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
BBMINFO