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

Shell Sort - Implementation in PHP

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

Shell Sort is a complex and fast sorting algorithm that repeatedly divides the entire collection into sub-collections by taking every h-th element for a fixed gap h and performs an insertion sort each sub-collection. The Shell Sort algorithm was published by Donald Shell in 1959.

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

1. Set a step size h that is smaller than the number of elements to be sorted, and greater that 1.

2. Group the entire collection of data elements in h groups by putting elements that are h steps away from each other into the same group.

3. Sort each group by exchanging locations of elements in the same group.

4. Repeat step 2 and 3 with a smaller step size h until h becomes 1.

This idea of sorting is based on the following facts:

- Sorting h groups of n/h elements in each group takes much less time than sorting n elements.
- If a collection is sorted with a step size h, it is called partially sorted, because any elements will be at most n-h steps away from it's final sorted position.
- If a collection is sorted with a step size h, the insertion sort will be much more efficient than the original collection.
- Insertion sort method should be used for each sorting step.
- The performance of this sorting method is strongly depending on the selection of grouping step sizes used in the sorting steps. One of the popular methods of selecting the grouping step sizes is to use this sequence: h(n+1) = 3*h(n) + 1.

Note that:

- If you replace h by 1, the code should identical to the insertion sort.

Here is my PHP implementation of Heap Sort algorithm:

<?php #- Sort_Functions.php #- Copyright (c) 2015 HerongYang.com. All Rights Reserved. #- function shellSort(&$a, $fromIndex, $toIndex) { $h = 1; while ($h<$toIndex-$fromIndex) $h = 3*$h + 1; while ($h>1) { $h = intval($h/3); for ($i=$fromIndex+$h; $i<$toIndex; $i++) { $d = $a[$fromIndex+$i]; $j = $i; while ($j>=$fromIndex+$h && $d<$a[$fromIndex+$j-$h]) { $a[$fromIndex+$j] = $a[$fromIndex+$j-$h]; $j = $j - $h; } $a[$fromIndex+$j] = $d; } } } # Functions for other sorting algorithms ... ?>

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

Array size: 10000 Average sorting time: 40.309 milliseconds Number of tests: 1000 Performance: 4.0309 O(N) microseconds Performance: 0.30335545238049 O(N*Log2(N)) microseconds Performance: 0.00040309 O(N*N) microseconds Array size: 20000 Average sorting time: 106.153 milliseconds Number of tests: 1000 Performance: 5.30765 O(N) microseconds Performance: 0.37148354187176 O(N*Log2(N)) microseconds Performance: 0.0002653825 O(N*N) microseconds Array size: 30000 Average sorting time: 177.27 milliseconds Number of tests: 1000 Performance: 5.909 O(N) microseconds Performance: 0.39730580057516 O(N*Log2(N)) microseconds Performance: 0.00019696666666667 O(N*N) microseconds

Here is the comparison of mergeSort() 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 Merge Sort 28 60 93 Heap Sort 47 100 160 Shell Sort 40 106 177 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