PHP and sorting by maximum
Sorting algorithms are classical methods for manipulating a set of objects to obtain an ordered sequence of these objects.
In general, we consider a set of elements of the same class on which a total order is defined, making all these elements comparable for a certain order relation denoted by <. If these objects are numbers, numerical comparison is straightforward. If, for example, these objects are students, the name can be considered as a character string on which lexicographic order (dictionary order) can be applied. The goal is to order these elements, meaning to perform a permutation so that the resulting set is ordered in ascending or descending order, based on the chosen order relation. The chosen method for ordering the elements constitutes a sorting algorithm.
There are many sorting algorithms because the problem is not as simple as it may seem. The challenge is to obtain a sequence, for example, in ascending order, of the elements to be sorted in a reasonable time, or even in a very short time.
In this article, we will consider an array of real numbers and sort them by rearranging them to obtain the elements in ascending order within the same array.
To achieve this, we define a class that includes a method for displaying the array, a method for exchanging two elements, and sorting methods.
The first method we will define involves sorting the array in ascending order using the concept of the maximum element that must be placed in its correct position (sorting by max).
<?php
class TableauReelTReel {
private $tableauReel = array();
private $tailleTableau = 0;
public function __construct(){}
// Afficher les éléments du tableau
public function afficher(){
$indice = 0;
$nbIteration = 0;
$nbIteration = count($this->tableauReel);
while($indice < $nbIteration)
{
echo $this->tableauReel[$indice];
$indice++;
echo '
';
}
}
// Ajouter des items au tableau
public function add($item) {
array_push($this->tableauReel, $item);
}
// Echanger les éléments du yableau
public function echanger($pos1, $pos2) {
$this->tailleTableau = count($this->tableauReel);
$reelTmp = 0;
if( ($pos1 > ($this->tailleTableau-1 ) ) || $pos2 > ($this->tailleTableau-1 ) ) {
echo 'Impossible';
}
else {
$reelTmp = $this->tableauReel[$pos1];
$this->tableauReel[$pos1] = $this->tableauReel[$pos2];
$this->tableauReel[$pos2] = $reelTmp;
}
}
// Tri par le max
public function triMax(){
$indice = $posMax = 0;
$max = $element1 = 0;
$nbIteration = count($this->tableauReel) - 1;
while($nbIteration >= 1) {
$max = $this->tableauReel[0];
$posMax = 0;
$indice = 1;
while($indice <= $nbIteration) {
$element1 = $this->tableauReel[$indice];
if($element1 > $max) {
$max = $this->tableauReel[$indice];
$posMax = $indice;
}
$indice++;
}
echo 'Position du max ='.$posMax.' et nombre d\'iteration ='.$nbIteration. '
';
$this->echanger($posMax, $nbIteration);
$this->afficher();
$nbIteration--;
}
}
}
// ---------------------------------------------------------
$tableau = new TableauReelTReel();
$tableau->add(45);
$tableau->add(3.14);
$tableau->add(3.24);
$tableau->add(-1.14);
$tableau->add(4.14);
// Afficher tableau
$tableau->afficher();
echo '
';
$tableau->triMax();
?>
In this example, we have a non-empty array of real numbers with a dimension of 5. We traverse the array (with the index ranging from 0 to 4) to find the index of the largest element (which we will place in a variable called max).
We then exchange the position of this element (max) with the last element of the array (the normal position of max). The maximum element will be in its correct position, and the exchanged element that occupied this position will be reconsidered.
Once max is in place, we repeat the scan for all elements in the array before it, i.e., the elements with indices ranging from 0 to 3. We’ll call a prefix the sub-array containing the initial elements with indices from 0 to the index before max. This process continues on progressively smaller prefixes, i.e., with indices ranging from 0 to 2, then from 0 to 1, until there are no more elements whose position needs to be exchanged with that of another element.
Preview of the code result :