Thursday, August 24, 2023

Sorting numbers without using inbuilt function

 $result = ['9','2','5','4','3','7'];

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

  for ($j = $i + 1; $j < count($result); $j++) {

    if ($result[$i] > $result[$j]) {

      $temp = $result[$i];

      $result[$i] = $result[$j];

      $result[$j] = $temp;

    }

  }

}


echo "<pre>";print_r($result);echo "</pre>";

OUTPUT:

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 7
    [5] => 9
)

No comments:

Post a Comment