Skip to content

Instantly share code, notes, and snippets.

@ring0li
Last active August 19, 2018 08:08
Show Gist options
  • Save ring0li/1c27e3d42bdeac47a046633cbbcfdeb0 to your computer and use it in GitHub Desktop.
Save ring0li/1c27e3d42bdeac47a046633cbbcfdeb0 to your computer and use it in GitHub Desktop.
算法
<?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