Tuesday 15 September 2015

Sorting arrays manually in php without using sort() -



Sorting arrays manually in php without using sort() -

help. stucked in logic , procedures in printing sorted array lists without using sort().

<?php $item = array(2, 1, 3); $item_length = count($item); ($counter = 0; $counter < $item_length; $counter++) { if ($item[$counter] <= $item[$counter + 1]) { print $item[$counter]; // should do?? } } ?>

here solution using bubble sort

<?php $item = array(2, 1, 4,3,5,6); $item_length = count($item); ($counter = 0; $counter < $item_length-1; $counter++) { ($counter1 = 0; $counter1 < $item_length-1; $counter1++) { if ($item[$counter1] > $item[$counter1 + 1]) { $temp=$item[$counter1]; $item[$counter1]=$item[$counter1+1]; $item[$counter1+1]=$temp; } } } //you can print array using loop print_r($item); ?>

php sorting

No comments:

Post a Comment