# How to implement the function getmin?

Help to solve the problem
To implement the function getMin(\$a). \$a – array of numbers. Sample output: the minimum number in the array (using the min function, the key array may be associative).
March 20th 20 at 11:19
March 20th 20 at 11:21
``````<?php

\$arr=array("a"=>5, "b"=>10, "c"=>3);

function getmin(\$a)
{
asort(\$a);

return reset(\$a);
}

echo getmin(\$arr);
?>``````
@connor.Jones, and about my version of what would you say ? - Bessie76 commented on March 20th 20 at 11:54
The method is certainly simple, but quite labor intensive.
I wouldn't take the option for a good solution to the problem. :) - Bette commented on March 20th 20 at 11:24
@connor.Jones, well, hold that thought then). - Sabrina70 commented on March 20th 20 at 11:27
@connor.Jones, and that it is labor-intensive? asort or return? - lucy commented on March 20th 20 at 11:30
@Ford_Hoppein the worst case, the complexity of sorting algorithm asort will be more O(n). While finding the minimum value should be no more. - Bette commented on March 20th 20 at 11:33
@connor.Jones, there is quicksort, so in the worst case O(n2), at best O(n * log n). In the end just too find the minimum values will be better, but - Crystal.Hil commented on March 20th 20 at 11:36
@connor.Jones, go to school and learn.

fuck. well, what else can I say, pioneers? - lucy commented on March 20th 20 at 11:39
@Ford_Hoppe,
fuck. well, what else can I say, pioneers?

For example, from which we can benefit, and not to plant a flame. - Bette commented on March 20th 20 at 11:42
@connor.Jones, you're here to read it himself: "in the worst case, the complexity of sorting algorithm asort will be more O(n). While finding the minimum value should be no more."

He realized that he wrote?

And who's flamet? - lucy commented on March 20th 20 at 11:45
@Ford_Hoppe,
He realized that he wrote?

Yes, it is clearly written.
Sorting will always consume more computational resources than conventional bypass array, with which, and need to implement a getmin function. - Bette commented on March 20th 20 at 11:48
@connor.Jones, convinced. But if it's a array of two thousand elements, attention to even such details to draw a sin. - lucy commented on March 20th 20 at 11:51
@connor.Jones, although in fact here is the extra challenge of the circuit, I do not know PHP at speed with this, but in Java it fast..
Although simple foreach would be much faster, unless you consider that it creates a copy of the array in recent versions of PHP that can also give a stupid speed - Bessie76 commented on March 20th 20 at 11:57
@connor.Jones, and in asort if you pass an array, and when will it change, will create a copy... but then comes the challenge serznyh cycles within the implementation of asort that will be faster to sort the array elements and sort (the matter only to the implementation of the sorter in asort)
did anyone benchmarks - Bessie76 commented on March 20th 20 at 12:00
@connor.Jones, you make use of at least one of PHP or JS framework? At least gaquere? The long array is sorted? Are you serious?))

Offer to write websites in Assembly language. - Sabrina70 commented on March 20th 20 at 12:03
@Gunner21, but that's a function getmin at the semantics levels of the framework, and is implemented at the level of the core language libraries. And homework theoretical, because in practice you will use the function min()instead of crutches. And now, based on these assumptions, the implementation of sorting is fundamentally not true.

P. S. I understand that in practice it will be irrelevant, but here it is still about the theory. - Bette commented on March 20th 20 at 12:06
March 20th 20 at 11:23
What are you bathing then:
``````function getmin(array \$arr) {
return array_reduce(\$arr, function(\$min, \$item) {
return \$item<\$min?\$item:\$min;
}, PHP_INT_MAX);
}``````

Array:
``var_dump(getmin(array("a"=>5, "b"=>10, "c"=>3))); = int(3)``

Empty:
``var_dump(getmin(array())); = int(9223372036854775807)``
March 20th 20 at 11:25
``````function getMin(\$a){
foreach (\$a as \$key => \$value) {
if(!isset(\$min)){
\$min = \$value;
}else{
if(\$min > \$value ){
\$min = \$value;
}
}
}
return \$min;
}``````
But if the array is empty? ;) - ashtyn commented on March 20th 20 at 11:28
@lexus_Ha, notice - Bessie76 commented on March 20th 20 at 11:31

Find more questions by tags PHP