code_junkie

Need to get specific indexes out of a PHP array

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. function select(list[1..n], k)
  2. for i from 1 to k
  3. maxIndex = i
  4. maxValue = list[i]
  5. for j from i+1 to n
  6. if list[j] > maxValue
  7. maxIndex = j
  8. maxValue = list[j]
  9. swap list[i] and list[maxIndex]
  10. return list[k]
  11.  
  12. newarray[] = select(array, 1);
  13. newarray[] = select(array, 2);
  14. newarray[] = select(array, 3);
  15.  
  16. function threeLargest($array){
  17. krsort($array, "SORT_NUMERIC");
  18. $return[0] = $array[0];
  19. $return[1] = $array[1];
  20. $return[2] = $array[2];
  21. return $return;
  22. }
Add Comment
Please, Sign In to add comment