Skip to content

Instantly share code, notes, and snippets.

@jougene
Last active April 13, 2017 08:23
Show Gist options
  • Save jougene/5e5902b7236ef636af20cb951391acaa to your computer and use it in GitHub Desktop.
Save jougene/5e5902b7236ef636af20cb951391acaa to your computer and use it in GitHub Desktop.
sort algorithms
<?php
// it is just a inversed version of bubble sort
// and elements dive to the dhische instead of go up like a bubble
function bubbleSort(&$array) {
$n = count($array);
for($j = 0; $j < $n; $j++) {
// inner loop
for($i = $n; $i > $j + 1; $i--) {
if($array[$i-2] > $array[$i-1]) {
swap($array[$i-2], $array[$i-1]);
}
}
}
return $array;
}
$testArray = [2, 5, 1, 7, 3, 1, 2, 1, 0, 0, 1];
print_r(bubbleSort($testArray));
function swap (&$x, &$y) {
$buf = $x;
$x = $y;
$y = $buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment