Answers for "quic sort algorithm in php"

PHP
4

quick sort php

<?php
function quick_sort($my_array)
 {
	$loe = $gt = array();
	if(count($my_array) < 2)
	{
		return $my_array;
	}
	$pivot_key = key($my_array);
	$pivot = array_shift($my_array);
	foreach($my_array as $val)
	{
		if($val <= $pivot)
		{
			$loe[] = $val;
		}elseif ($val > $pivot)
		{
			$gt[] = $val;
		}
	}
	return array_merge(quick_sort($loe),array($pivot_key=>$pivot),quick_sort($gt));
}
Posted by: Guest on July-15-2020
2

php sorting functions

<?php
$fruit = array("apple","banana","mango","orange","strawbary");

sort($fruit);       //arrange in ascending order
echo "<pre>";
print_r($fruit);

rsort( $fruit);     //sort in descending order
foreach($fruit as $val)
{
    echo $val."<br>";
}

$girl = array("krisha"=>20,"yashvi"=>30,"ritu"=>4,"pinal"=>80);
asort($girl);       //sort in ascending order according to value
print_r($girl);

ksort($girl);   //sort in ascending order according to key
print_r($girl);     

arsort($girl);      //sort in descending order according to value
print_r($girl);

krsort($girl);      //sort in descending order according to key
print_r($girl);
?>
Posted by: Guest on September-06-2021

Browse Popular Code Answers by Language