**Sorting Algorithm Tutorials - Herong's Tutorial Examples** - 6.12, by Herong Yang

Bubble Sort - Implementation in PHP

This section provides a tutorial on how to implement the Bubble Sort algorithm in PHP.

Bubble Sort is a simple and slow sorting algorithm that repeatedly steps through the collection, compares each pair of adjacent elements and swaps them if they are in the wrong order.

The basic idea of Bubble Sort algorithm can be described as these steps:

1. Data elements are grouped into two sections: a sorted section and an un-sorted section.

2. Go through every element in the un-sorted section and re-arrange its position with its neighbor to put the element with higher order on the higher position. At the end, the element with the highest order will be on top of the un-sorted section, and moved to the bottom of the sorted section.

3. Repeat step 2 until no more elements left in the un-sorted section.

In the sorting algorithm, if you watch the move of the elements with higher orders, they are like bubbles in the water, floating slowly from the bottom to the top.

Here is my PHP implementation of Bubble Sort algorithm:

<?php #- Sort_Functions.php #- Copyright (c) 2015 HerongYang.com. All Rights Reserved. #- function bubbleSort(&$a, $fromIndex, $toIndex) { for ($i=$toIndex-1; $i>$fromIndex; $i--) { $isSorted = true; for ($j=$fromIndex; $j<$i; $j++) { if (($a[$j])>$a[$j+1]) { $isSorted = false; $d = $a[$j+1]; $a[$j+1] = $a[$j]; $a[$j] = $d; } } if ($isSorted) break; } } # Functions for other sorting algorithms ... ?>

The following diagram illustrates how this implementation works:

--17 42 53 67 ... 92 | | | | | +---+---+---------------+---+---+---+---+---+---------------+ | | | / 29 36 11 ... 24 39 | | | | fromIndex j i toIndex-1

Note that:

- Elements to be sorted are stored from "fromIndex" to "toIndex-1" inclusive.
- At the beginning of each iteration of loop "i", elements from "i+1" to "toIndex-1" are sorted.
- At the beginning of each iteration of loop "i", elements from "fromIndex" to "j+1" are not sorted.
- At the end of each iteration of loop "i", element at "i" will be the element with the highest order in the un-sorted section.
- The "break" statement is there just in case when the un-sorted section happen to be already sorted.

Here are the performance test results of bubbleSort() function using PHP 5.6.

Array size: 10000 Average sorting time: 6846.6 milliseconds Number of tests: 10 Performance: 684.66 O(N) microseconds Performance: 51.525799207825 O(N*Log2(N)) microseconds Performance: 0.068466 O(N*N) microseconds Array size: 20000 Average sorting time: 28426.6 milliseconds Number of tests: 5 Performance: 1421.33 O(N) microseconds Performance: 99.479186187595 O(N*Log2(N)) microseconds Performance: 0.0710665 O(N*N) microseconds Array size: 30000 Average sorting time: 66524 milliseconds Number of tests: 1 Performance: 2217.4666666667 O(N) microseconds Performance: 149.09669474509 O(N*Log2(N)) microseconds Performance: 0.073915555555556 O(N*N) microseconds

Here is the comparison of bubbleSort() performance with other sorting functions. As you can see, Bubble Sort is much slower than other sorting functions.

Array Size 10000 20000 30000 100000 200000 300000 ---------- ----- ----- ----- ------ ------ ------ JDK Arrays.sort 25 66 112 PHP sort() 3 7 13 75 Insertion Sort 2213 9484 23329 Selection Sort 3580 14129 33808 Bubble Sort 6847 28427 66524

Table of Contents

Introduction of Sorting Algorithms

Java API for Sorting Algorithms

Insertion Sort Algorithm and Java Implementation

Selection Sort Algorithm and Java Implementation

Bubble Sort Algorithm and Java Implementation

Quicksort Algorithm and Java Implementation

Merge Sort Algorithm and Java Implementation

Heap Sort Algorithm and Java Implementation

Shell Sort Algorithm and Java Implementation

►Sorting Algorithms Implementations in PHP

Sort_Test.php - Sorting Performance Test

Insertion Sort - Implementation in PHP

Selection Sort - Implementation in PHP

►Bubble Sort - Implementation in PHP

Quicksort - Implementation in PHP

Merge Sort - Implementation in PHP

Heap Sort - Implementation in PHP

Shell Sort - Implementation in PHP

Sorting Algorithms Implementations in Perl

Sorting Algorithms Implementations in Python