Last active
April 13, 2017 08:23
-
-
Save jougene/5e5902b7236ef636af20cb951391acaa to your computer and use it in GitHub Desktop.
sort algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// it is just a inversed version of bubble sort | |
// and elements dive to the dhische instead of go up like a bubble | |
function bubbleSort(&$array) { | |
$n = count($array); | |
for($j = 0; $j < $n; $j++) { | |
// inner loop | |
for($i = $n; $i > $j + 1; $i--) { | |
if($array[$i-2] > $array[$i-1]) { | |
swap($array[$i-2], $array[$i-1]); | |
} | |
} | |
} | |
return $array; | |
} | |
$testArray = [2, 5, 1, 7, 3, 1, 2, 1, 0, 0, 1]; | |
print_r(bubbleSort($testArray)); | |
function swap (&$x, &$y) { | |
$buf = $x; | |
$x = $y; | |
$y = $buf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment