How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PHP programming language
How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PHP programming language
Table of Contents
Problem Statement
Sort an array (or list) of elements using the Selection sort algorithm.
It works as follows: First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
Its asymptotic complexity is O(n2) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM. No other sorting algorithm has less data movement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PHP programming language
The code provided includes two versions of the selection sort algorithm:
Version 1 - Using For Loops:
This version of the algorithm takes a reference to an array as input and performs the following steps:
- Initialize a variable
$n
to the count of elements in the array. - Iterate over each element in the array using a
for
loop from$i = 0
to$n
. - Within this loop, find the index of the minimum element in the remaining unsorted portion of the array. To do this, initialize
$min
to$i
and iterate through the remaining elements (starting from$j = $i + 1
). If any element is found to be less than the current minimum, update$min
to the index of that element. - Swap the element at index
$i
with the element at index$min
to put the minimum element in its correct position.
Version 2 - Using Recursion:
This version uses recursion to implement the selection sort algorithm:
- It takes two arguments: the input array
$arr
and an optional$result
array (which is initially empty). - If the input array is empty, it returns the
$result
array. - It finds the minimum value in the
$arr
array and adds it to the$result
array. - It removes the minimum value from the
$arr
array and calls theselectionsort
function recursively with the modified array and the updated$result
array.
This recursive version continues until the input array is empty, at which point it returns the sorted $result
array.
Source code in the php programming language
function selection_sort(&$arr) {
$n = count($arr);
for($i = 0; $i < count($arr); $i++) {
$min = $i;
for($j = $i + 1; $j < $n; $j++){
if($arr[$j] < $arr[$min]){
$min = $j;
}
}
list($arr[$i],$arr[$min]) = array($arr[$min],$arr[$i]);
}
}
function selectionsort($arr,$result=array()){
if(count($arr) == 0){
return $result;
}
$nresult = $result;
$nresult[] = min($arr);
unset($arr[array_search(min($arr),$arr)]);
return selectionsort($arr,$nresult);
}
You may also check:How to resolve the algorithm Long primes step by step in the 11l programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the BQN programming language
You may also check:How to resolve the algorithm Permutations/Derangements step by step in the jq programming language
You may also check:How to resolve the algorithm Jacobsthal numbers step by step in the Rust programming language