PHP Sort Array without using built in function

We can write our own program to sort array in PHP without using built in functions like – sort(), rsort().

Example

<?php

$array = array(21, 9, 10, 11, 9);

echo "Array Unsorted<br />";
print_r($array);


for($i = 0; $ < count($array); $i ++) {
    for($j = 0; $j < count($array)-1; $j ++){

        if($array[$j] > $array[$j+1]) {
            $temp = $array[$j+1];
            $array[$j+1]=$array[$j];
            $array[$j]=$temp;
        }       
    }
}

echo "Array After Sorting: ";
echo "<br />";
print_r($array);

?>

Advertisements

Add Comment