Ok, so how would you sort an array of numbers?
In PHP: sort($myArray);
In Javascript: myArray.sort();
Simple right? In my nearly 10 years in software development I have never once had the need to write a sorting algorithm. Yet for some stupid reason, I am invariably asked to write a sorting algorithm on a whiteboard at just about every interview I’ve ever had. Well, Here you go.. Study this useless piece of code so you can land your next job. Have fun!
function bubble_sort($arr) {
$size = count($arr);
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-1-$i; $j++) {
if ($arr[$j+1] < $arr[$j]) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $tmp;
}
}
}
return $arr;
}