Dark
Light
Solarized Light
Solarized

Bubble Sort Examples:

Java
Python
PHP
C#
C++
TypeScript
▸ Bubble Sort Quick Review

# Bubble Sort in PHP

``````&lt;?php

class BubbleSort {

public static function sort(&amp;\$arr) {
\$size = count(\$arr);

for(\$i = 0; \$i &lt; \$size - 1; \$i++) {
// For each element, loop over and find inversions.
for(\$j = 0; \$j &lt; \$size - \$i - 1; \$j++) {
// If i and j are inverted, swap them
if(\$arr[\$j] &gt; \$arr[\$j + 1]) {
BubbleSort::swap(\$arr, \$j, \$j + 1);
}
}
}
}

public static function sortFaster(&amp;\$arr) {

\$size = count(\$arr);

for(\$i = 0; \$i &lt; \$size - 1; \$i++) {
// Set swapped flag to false.
\$swapped = false;
// For each element, loop over and find inversions.
for(\$j = 0; \$j &lt; \$size - \$i - 1; \$j++) {
// If i and j are inverted, swap them
if(\$arr[\$j] &gt; \$arr[\$j + 1]) {
BubbleSort::swap(\$arr, \$j, \$j + 1);
// set the swap flag to true.
\$swapped = true;
}
}
// Check for any swaps
if(!\$swapped) {
// If not, we are done.
break;
}
}
}

public static function swap(&amp;\$arr, \$a, \$b) {
\$tmp = \$arr[\$a];
\$arr[\$a] = \$arr[\$b];
\$arr[\$b] = \$tmp;
}
}
``````

Find any bugs in the code? let us know!