Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <?php
  2. // execute command: php bubble-sorting.php 5 0 2
  3. echo 'bubble sorting'.PHP_EOL;
  4.  
  5. $sort_array = array();
  6. for ($i = 1; $i <= count($argv); $i++){
  7. $value = $argv[$i];
  8. if (preg_match('/^(--)/', $value)) {
  9. $value = substr($value, 2);
  10. $$value = true;
  11. }
  12. if (is_numeric($value))
  13. array_push($sort_array, $value);
  14. }
  15.  
  16. bubble_sorting($sort_array, $detail);
  17. var_dump($sort_array);
  18.  
  19. function bubble_sorting(&$sort_array){
  20. global $detail, $reverse;
  21. $loop_count = count($sort_array);
  22. for ($i = 0; $i < $loop_count; $i++){
  23. for ($j = 0; $j < $loop_count-1; $j++){
  24. if ($detail)
  25. print_r($sort_array);
  26. if (!$reverse && $sort_array[$j] > $sort_array[($j+1)])
  27. swap_positions($sort_array[$j], $sort_array[($j+1)]);
  28. else if ($reverse && $sort_array[$j] < $sort_array[($j+1)])
  29. swap_positions($sort_array[$j], $sort_array[($j+1)]);
  30. }
  31. }
  32. }
  33.  
  34. function swap_positions(&$first_element, &$second_element){
  35. list($first_element, $second_element) = array($second_element, $first_element);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement