fork(2) download
  1. <?php
  2.  
  3. // your code goes here
  4.  
  5. $arr = array(2,3,4,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12);
  6. $newArr= array();
  7. function find($arr, $current) {
  8. $i = 0;
  9. $len = count($arr);
  10. $mini = $arr[0];
  11. $minipos = 0;
  12. for ($i = 0; $i < $len; $i++) {
  13. if ($arr[$i] < $mini) {
  14. $mini = $arr[$i];
  15. $minipos = $i;
  16. }
  17. if ($arr[$i] == $current + 1) {
  18. return $i;
  19. }
  20. }
  21. return $minipos;
  22. }
  23.  
  24. $current = -1;
  25. while (count($arr)) {
  26. $pos = find($arr, $current);
  27. $current = $arr[$pos];
  28. /*echo $pos.'<br/>';*/
  29. array_push($newArr, $arr[$pos]);
  30. array_splice($arr, $pos, 1);
  31. }
  32.  
  33. echo json_encode($newArr);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
[2,3,4,5,6,7,8,9,10,11,12,6,7,8,9,10,11,12]