Quicksort - Implementation in PHP

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

Quicksort is a complex and fast sorting algorithm that repeatedly divides an un-sorted section into a lower order sub-section and a higher order sub-section by comparing to a pivot element. The Quicksort algorithm was developed in 1960 by Tony Hoare while in the Soviet Union, as a visiting student at Moscow State University.

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

1. Select an element as a pivot element.

2. Data elements are grouped into two sections: one with elements that are in lower order than the pivot element, one with element that are in higher order than the pivot element.

3. Sort the two sections separately by repeating step 1 and 2.

Obviously, this is a recursive idea, where a problem is divided into smaller problems. And the division will be repeated to make the smaller problems even smaller, until they are smaller enough so that the solution is obvious.

Here is my PHP implementation of Quicksort algorithm:

<?php
#- Sort_Functions.php
#- Copyright (c) HerongYang.com. All Rights Reserved.
#-
function quickSort(&$a, $fromIndex, $toIndex) {
   if ($toIndex-$fromIndex<=1) {        # only 1 elements
      return;
   } else if ($toIndex-$fromIndex==2) { # 2 elements
      if (($a[$fromIndex])>$a[$toIndex-1]) {
         $d = $a[$toIndex-1];
         $a[$toIndex-1] = $a[$fromIndex];
         $a[$fromIndex] = $d;
      }
   } else {                             # 3 or more elements
      $p = $a[$fromIndex];              # the pivot value
      $iLeft = $fromIndex + 1;
      $iRight = $toIndex - 1;
      while ($iLeft<$iRight) {
         while ($iLeft<$toIndex-1 && $p>=$a[$iLeft]) {
            $iLeft++;                   # most left element that > pivot
         }
         while ($iRight>$fromIndex+1 && $p<=$a[$iRight]) {
            $iRight--;                  # most right element that < pivot
         }
         if ($iLeft>=$iRight) break;
         $d = $a[$iRight];              # swap them and continue
         $a[$iRight] = $a[$iLeft];
         $a[$iLeft] = $d;
      }
      if ($p>$a[$iRight]) {              # have elements < pivot
        $d = $a[$iRight];                # swap a[iRight] with pivot
        $a[$iRight] = $a[$fromIndex];
        $a[$fromIndex] = $d;
      } else {                           # no element < pivot
        $iRight--;
      }
      if ($fromIndex<$iRight-1) quickSort($a, $fromIndex, $iRight);
      if ($iLeft<$toIndex-1) quickSort($a, $iLeft, $toIndex);
   }
}

# Functions for other sorting algorithms ...
?>

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

Array size: 10000
Average sorting time: 20.4 milliseconds
Number of tests: 10
Performance: 2.04 O(N) microseconds
Performance: 0.15352529778863 O(N*Log2(N)) microseconds
Performance: 0.000204 O(N*N) microseconds

Array size: 20000
Average sorting time: 42.4 milliseconds
Number of tests: 10
Performance: 2.12 O(N) microseconds
Performance: 0.14837924670393 O(N*Log2(N)) microseconds
Performance: 0.000106 O(N*N) microseconds

Array size: 30000
Average sorting time: 76.3 milliseconds
Number of tests: 10
Performance: 2.5433333333333 O(N) microseconds
Performance: 0.17100712237764 O(N*Log2(N)) microseconds
Performance: 8.4777777777778E-5 O(N*N) microseconds

Here is the comparison of quickSort() performance with other sorting functions. As you can see, Quicksort is much faster than other sorting functions.

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

Table of Contents

 About This Book

 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

 Performance Summary of All Implementations

 References

 Full Version in PDF/EPUB