Last active
August 19, 2018 08:08
-
-
Save ring0li/1c27e3d42bdeac47a046633cbbcfdeb0 to your computer and use it in GitHub Desktop.
算法
This file contains 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 | |
/** | |
* 冒泡排序 | |
*/ | |
function bubble_sort(array &$ar) | |
{ | |
$tmp = 0; | |
$count = count($ar); | |
for ($i = 0; $i < $count - 1; $i++) { | |
for ($j = 0; $j < $count - 1 - $i; $j++) { | |
if ($ar[$j] > $ar[$j + 1]) { | |
$tmp = $ar[$j]; | |
$ar[$j] = $ar[$j + 1]; | |
$ar[$j + 1] = $tmp; | |
} | |
} | |
} | |
} | |
$ar = array(21, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70); | |
bubble_sort($ar); | |
print_r($ar); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment