What are the different functions in sorting an array
Q:What are the different functions in sorting an array? Answer: The functions for sorting array are following: 1- sort($array)- is used for enumerated array(numeric array) sorts...
Q:What are the different functions in sorting an array?
Answer: The functions for sorting array are following: 1- sort($array)- is used for enumerated array(numeric array) sorts the array element in ascending order. 2- rsort($array)- is used for enumerated array,sorts the array element in descending order. 3- asort($array)- is used for associative array,sorts the array value in ascending order. 4- arsort($array)- is used for associative array,sorts the array value in descending order. 5- ksort($array)- is used for associative array,sorts the array keys in ascending order. 6- krsort($array)- is used for associative array,sorts the array keys in descending order.These functions usage can be fairly similar with just a slight modification I will explain two functions usage here:
$Nlength=count($naa);
for($x=0;$x<$Nlength;$x++)
{
echo $naa[$x];
echo "
";
}
The output of this would be?
Here is the output result: Meena Teena Xena
Example 2:
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
echo $numbers[$x];
echo "
";
}
?>
Result output: 3 4 8 13 19
krsort() Function: The krsort() function sorts an associative array in descending order, according to the key. (i.e sorting an array in ascending order)
"35","Mango"=>"37","Banana"=>"43");
krsort($price);
foreach($price as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
Out Put: Key=Orange, Value=35 Key=Mango, Value=43 Key=Banana, Value=37
Use the arsort() function to sort an associative array in descending order, according to the value (i.e sorting an array in descending order)
Interviewer Evaluation Points & Scoring Rubric
- 1 Analyze Time Complexity (Big O) and Space Complexity explicitly.
- 2 Address boundary cases: empty input, null pointers, single elements.
- 3 Write idiomatic, readable code with descriptive variable naming.