Open In App

How to Sort an Array containing 1 to n values in PHP ?

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to sort an array that contains 1 to n values in PHP. There are various approaches through which we can sort an array with custom values. Below is an example for a better understanding of the problem statement.

Example:

Input: arr = [3, 1, 4, 5, 2, 8, 9, 5]
Output: Sorted Array is: [1, 2, 3, 4, 5, 8, 9]
 
Input: arr = [3, 1, 4, 5, 2, 0, 10]
Output: Sorted Array is: [0, 1,  2, 3, 4, 5, 10]

There are two methods to sort an array that contains 1 to n values:

Using PHP sort() Function

We will use the built-in sort() function to sort the input array into ascending to descending order. It sorts the actual array and hence changes are reflected in the original array itself. 

Syntax

sort(array &$array, int $sort_flags = SORT_REGULAR): bool

Example: This example uses the built-in sort() function to sort the input array into the correct order.

PHP




<?php
function sortArray($arr)
{
    sort($arr);
    return $arr;
}
$arr1 = [3, 1, 4, 5, 2, 8, 6, 10, 7];
$arr2 = [3, 1, 4, 5, 2, 0, 10];
$result1 = sortArray($arr1);
$result2 = sortArray($arr2);
echo "Sorted Array is: [" . implode(", ", $result1) . "]\n";
echo "Sorted Array is: [" . implode(", ", $result2) . "]\n";
?>


Output

Sorted Array is: [1, 2, 3, 4, 5, 6, 7, 8, 10]
Sorted Array is: [0, 1, 2, 3, 4, 5, 10]

Using a Custom Sorting Algorithm (Bubble Sort)

We will use the Bubble sort algorithm to manually sort the array without relying on any built-in approach to sort the array. Here, the loops are used to iterate over the array elements and sort them in the correct order.

Syntax

function bubbleSort(&$arr) {
    $n = count($arr);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = 0; $j < $n - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
            
                // Swap $arr[$j] and $arr[$j+1]
                $temp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $temp;
            }
        }
    }
}

Example: This example uses the custom sorting algorithm (Bubble Sort) to sort the input array elements.

PHP




<?php
function customSortArray($arr)
{
    $n = count($arr);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = 0; $j < $n - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $temp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $temp;
            }
        }
    }
    return $arr;
}
$arr1 = [3, 1, 4, 5, 2, 8, 6, 10, 7];
$arr2 = [3, 1, 4, 5, 2, 0, 10];
$result1 = customSortArray($arr1);
$result2 = customSortArray($arr2);
echo "Sorted Array is: [" . implode(", ", $result1) . "]\n";
echo "Sorted Array is: [" . implode(", ", $result2) . "]\n";
?>


Output

Sorted Array is: [1, 2, 3, 4, 5, 6, 7, 8, 10]
Sorted Array is: [0, 1, 2, 3, 4, 5, 10]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads