Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created March 25, 2019 10:25
Show Gist options
  • Save fyulistian/51b9cbe7f8755588e86abbbe3c0e41e7 to your computer and use it in GitHub Desktop.
Save fyulistian/51b9cbe7f8755588e86abbbe3c0e41e7 to your computer and use it in GitHub Desktop.
Buble sort in php
function bubble_sort($sortarr) {
// Bubble sorting
$array_count = count($sortarr);
for($x = 0; $x < $array_count; $x++){
for($a = 0 ; $a < $array_count - 1 ; $a++){
if($a < $array_count ){
if($sortarr[$a] > $sortarr[$a + 1]) swap($sortarr, $a, $a+1);
}
}
}
return $sortarr;
}
function swap(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment