Saturday, June 19, 2021

Interview question in php

Question :-  

Sort an array without using any sorting function in php

Input : - $array = array(9, 2, 18, 34, 3, 10, 15);

Output : - $array = array(2,3,9, 10,15,18,34);
<?php
 
// take an array with some elements
$array = array(9, 2, 18, 34, 3, 10, 15);
// get the size of array
$count = count($array);
echo "<pre>";
// Print array elements before sorting
print_r($array);
for ($i = 0; $i < $count; $i++) {
    for ($j = $i + 1; $j < $count; $j++) {
        if ($array[$i] > $array[$j]) {
            $temp = $array[$i];
            $array[$i] = $array[$j];
            $array[$j] = $temp;
        }
    }
}
echo "Sorted Array:" . "<br/>";
// Print array elements after sorting
print_r($array);



No comments:

Post a Comment

If you have any problem please let me know.