Guest User

Median selection

a guest
May 1st, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.53 KB | None | 0 0
  1.    protected function selectPivot(array $dataset)
  2.    {
  3.       $x      = $dataset[0];
  4.       $y      = $dataset[(int)(count($dataset)/2)];
  5.       $z      = $dataset[count($dataset)-1];
  6.       if($x>=$y)
  7.       {
  8.          if($y>=$z)
  9.             return [(int)(count($dataset)/2) => $y];
  10.          return $x>=$z
  11.             ?[count($dataset)-1 => $z]
  12.             :[0 => $x];
  13.       }
  14.       if($x>=$z)
  15.          return [0 => $x];
  16.       return $y>=$z
  17.          ?[count($dataset)-1 => $z]
  18.          :[(int)(count($dataset)/2) => $y];
  19.    }
Advertisement
Add Comment
Please, Sign In to add comment