PHP

sorting array value without using built in php like sort() etc

Pinterest LinkedIn Tumblr

Sorting array value without using built in php like sort() etc

Bubble sort in php

<?php

$array =array('2','4','8','5','1','7','6','9','10','3');

echo 'Unsorted array';
print_r($array);

for($i = 0; $i<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 'Sorted array';
print_r($array);

?>

Reverse Order Sorting

<?php

$array = array('2','4','8','5','1','7','6','9','10','3');

echo 'Unsorted array';
print_r($array);

for($i = 0; $i<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 'Sorted array';
print_r($array);

?>

Write A Comment