PHP Program to sort elements of array in descending order


March 26, 2022, Learn eTutorial
967

Array in descending order

In this program, we are going to sort the elements of an array in descending order. To sort the elements in descending order we have to compare every element of the array with each other. For example, the array is 56,8,12,9,87  then the output will be 8,9,12,56,87.

How to sort elements of the array in descending order using PHP?

 For this program to sort the elements in an array we are taking the static values which are assigned to the array a[]. Then we have to assign the value 0 into the variable temp to store temporarily and also assign the size of the array into the variable len using the built-in function count() and after that, we have to assign the value 0 into the variable i and perform the loop until the condition 'i < len' and also increment the value of the variable i in every iteration and in the loop block we have to assign the value 1 into the variable j and perform the loop until the condition 'j < len' and also increment the value of the variable j in every iteration and in the loop block check the condition 'a[i] > a[j]' if true assign the value of a[i] into the variable temp, assign the value of a[j] into a[i] and assign the value of temp into a[j]  after the completion of the loops we can print the elements of the array in the descending order by using the for loop.

ALGORITHM

Step 1: Initialize an array a[] with values

Step 2: Create a temporary variable temp with a value of 0

Step 3: Assing the size of an array into the variable len using the built-in function count()

Step 4: Print the element in the array a[]

Step 5: Assign the value 0 into the variable i and perform the step 6 until the condition 'i < len' becomes false and increment the value of variable i in every iteration

Step 6: Assign the value 1 into the variable j and perform the sub-steps until the condition 'j < len' becomes false and increment the value of variable j in every iteration

        (i) Check the condition 'a[i] < a[j]' if true perform the sub-steps

            (a) Assign the value of a[i] into variable temp

            (b) Assign the value of a[j] into a[i]

            (c) Assign the value of temp into a[j]

Step 5: Print the elements of the array a[] as in ascending order

PHP Source Code

                                          <?php
$a = array(10, 8, 92, 69, 74);
$temp = 0;
$len = count($a);
echo "Elements in array are:\n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
for ($i = 0; $i < $len; $i++) {
    for ($j = $i + 1; $j < $len; $j++) {
        if ($a[$i] < $a[$j]) {
            $temp = $a[$i];
            $a[$i] = $a[$j];
            $a[$j] = $temp;
        }
    }
}
echo "\nElements of array sorted in ascending order: \n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
?>
                                      

OUTPUT

Elements in array are:
10 8 92 69 74
Elements of array sorted in ascending order:
92 74 69 10 8