Created
March 25, 2019 10:25
-
-
Save fyulistian/51b9cbe7f8755588e86abbbe3c0e41e7 to your computer and use it in GitHub Desktop.
Buble sort in php
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
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