Last active
December 12, 2015 12:09
-
-
Save ackintosh/4770442 to your computer and use it in GitHub Desktop.
Bubble sort in PHP
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 | |
| $array = range(1, 20); | |
| shuffle($array); | |
| var_dump($array); | |
| var_dump(bubblesort($array)); | |
| function bubblesort($array) | |
| { | |
| $size = count($array); | |
| for ($i = $size; $i > 0; $i--) { | |
| for ($j = 1; $j < $i; $j++) { | |
| if ($array[$j - 1] > $array[$j]) { | |
| list($array[$j - 1], $array[$j]) = array($array[$j], $array[$j - 1]); | |
| } | |
| } | |
| } | |
| return $array; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment