PHP Bubble Sort algorithm tutorial with examples for sorting numeric and textual data in ascending and descending order.
last modified April 16, 2025
An algorithm is a step-by-step procedure to solve a problem or perform a computation. In programming, algorithms are implemented as functions or methods.
Sorting is arranging data in a particular order, typically ascending or descending. Efficient sorting is crucial for optimizing other algorithms that require sorted input.
Common sorting algorithms include:
Bubble Sort
Quick Sort
Merge Sort
Insertion Sort
Selection Sort
Heap Sort
Bubble Sort is a simple comparison-based algorithm. It repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
The algorithm gets its name because smaller elements “bubble” to the top of the list. It has O(n²) time complexity, making it inefficient for large datasets.
Here’s a basic implementation of Bubble Sort in PHP for numeric data:
bubble_sort.php
<?php
function bubbleSort(array $arr): array { $n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
// Swap elements
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
$numbers = [64, 34, 25, 12, 22, 11, 90]; $sorted = bubbleSort($numbers);
print_r($sorted); // Output: Array ( [0] => 11 [1] => 12 [2] => 22 [3] => 25 [4] => 34 [5] => 64 [6] => 90 )
We can optimize Bubble Sort by adding a flag to check if any swaps were made in a pass. If no swaps occur, the array is already sorted.
optimized_bubble.php
<?php
function optimizedBubbleSort(array $arr): array { $n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
$swapped = false;
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
// Swap elements
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
$swapped = true;
}
}
// If no swaps, array is sorted
if (!$swapped) break;
}
return $arr;
}
$numbers = [5, 1, 4, 2, 8]; $sorted = optimizedBubbleSort($numbers);
print_r($sorted); // Output: Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 8 )
To sort in descending order, we simply reverse the comparison condition.
descending_bubble.php
<?php
function bubbleSortDesc(array $arr): array { $n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] < $arr[$j + 1]) { // Changed comparison operator
// Swap elements
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
$numbers = [64, 34, 25, 12, 22, 11, 90]; $sorted = bubbleSortDesc($numbers);
print_r($sorted); // Output: Array ( [0] => 90 [1] => 64 [2] => 34 [3] => 25 [4] => 22 [5] => 12 [6] => 11 )
Bubble Sort can also sort strings alphabetically by comparing them with the strcmp function.
text_bubble.php
<?php
function bubbleSortText(array $arr): array { $n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if (strcmp($arr[$j], $arr[$j + 1]) > 0) {
// Swap elements
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
$names = [“John”, “Alice”, “Bob”, “Eve”, “David”]; $sorted = bubbleSortText($names);
print_r($sorted); // Output: Array ( [0] => Alice [1] => Bob [2] => David [3] => Eve [4] => John )
Let’s compare Bubble Sort with the more efficient Quick Sort algorithm. We’ll measure execution time for sorting large arrays.
sort_benchmark.php
<?php
function bubbleSort(array $arr): array { $n = count($arr); for ($i = 0; $i < $n - 1; $i++) { for ($j = 0; $j < $n - $i - 1; $j++) { if ($arr[$j] > $arr[$j + 1]) { $temp = $arr[$j]; $arr[$j] = $arr[$j + 1]; $arr[$j + 1] = $temp; } } } return $arr; }
function quickSort(array $arr): array { if (count($arr) <= 1) return $arr;
$pivot = $arr[0];
$left = $right = [];
for ($i = 1; $i < count($arr); $i++) {
if ($arr[$i] < $pivot) {
$left[] = $arr[$i];
} else {
$right[] = $arr[$i];
}
}
return array_merge(quickSort($left), [$pivot], quickSort($right));
}
// Generate large array $largeArray = range(1, 2000); shuffle($largeArray);
// Benchmark Bubble Sort $start = microtime(true); bubbleSort($largeArray); $bubbleTime = microtime(true) - $start;
// Benchmark Quick Sort $start = microtime(true); quickSort($largeArray); $quickTime = microtime(true) - $start;
printf(“Bubble Sort time: %.4f seconds\n”, $bubbleTime); printf(“Quick Sort time: %.4f seconds\n”, $quickTime);
// Typical output: // Bubble Sort time: 1.2345 seconds // Quick Sort time: 0.0123 seconds
The benchmark clearly shows Quick Sort’s superior performance for large datasets. Bubble Sort’s O(n²) complexity makes it impractical for large arrays, while Quick Sort’s O(n log n) performs much better.
Despite its inefficiency, Bubble Sort has some use cases:
Educational purposes to understand sorting algorithms
When simplicity is more important than performance
For nearly sorted data (with the optimized version)
When working with very small datasets
This tutorial covered the Bubble Sort algorithm in PHP with examples for both numeric and textual data, including performance comparisons.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.