Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // execute command: php bubble-sorting.php 5 0 2
- echo 'bubble sorting'.PHP_EOL;
- $sort_array = array();
- for ($i = 1; $i <= count($argv); $i++){
- $value = $argv[$i];
- if (preg_match('/^(--)/', $value)) {
- $value = substr($value, 2);
- $$value = true;
- }
- if (is_numeric($value))
- array_push($sort_array, $value);
- }
- bubble_sorting($sort_array, $detail);
- var_dump($sort_array);
- function bubble_sorting(&$sort_array){
- global $detail, $reverse;
- $loop_count = count($sort_array);
- for ($i = 0; $i < $loop_count; $i++){
- for ($j = 0; $j < $loop_count-1; $j++){
- if ($detail)
- print_r($sort_array);
- if (!$reverse && $sort_array[$j] > $sort_array[($j+1)])
- swap_positions($sort_array[$j], $sort_array[($j+1)]);
- else if ($reverse && $sort_array[$j] < $sort_array[($j+1)])
- swap_positions($sort_array[$j], $sort_array[($j+1)]);
- }
- }
- }
- function swap_positions(&$first_element, &$second_element){
- list($first_element, $second_element) = array($second_element, $first_element);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement