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

Insertion Sort - Implementation in PHP

This section provides a tutorial on how to implement the Insertion Sort algorithm with the binary search method in PHP.

Insertion Sort is a simple and slow sorting algorithm that repeatedly takes the next element from the un-sorted section and inserts it into the sorted section at the correct position.

The basic idea of Insertion 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. Take an element from the un-sorted section.

3. Insert the element into the sorted section at the correct position based on the comparable property.

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

The idea of insertion sort comes from our daily life experiences. For example, when you play cards with your friends, you will insert the next card you pick into the sorted cards in your hand.

Here is my PHP implementation of the Insertion Sort algorithm in the Sort_Functions.php script. The binary search method was used to find the target of the insertion in the sorted section.

<?php #- Sort_Functions.php #- Copyright (c) 2015 HerongYang.com. All Rights Reserved. #- function insertionSort(&$a, $fromIndex, $toIndex) { for ($i=$fromIndex+1; $i<$toIndex; $i++) { $d = $a[$i]; $jLeft = $fromIndex; # left index of the target range for insertion $jRight = $i-1; # right index of the target range for insertion if ($a[$jRight]>$d) { # insertion is needed while ($jRight-$jLeft>=2) { # bring the target range to 1 or 2 $jMiddle = intval(($jRight-$jLeft)/2) + $jLeft - 1; if (($a[$jMiddle])>$d) { $jRight = $jMiddle; } else { $jLeft = $jMiddle + 1; } } if ($jRight-$jLeft==1) { # bring the target range to 1 $jMiddle = $jLeft; if (($a[$jMiddle])>$d) { $jRight = $jMiddle; } else { $jLeft = $jMiddle + 1; } } for ($j=$i; $j>$jLeft; $j--) { # the target of insertion is jLeft $a[$j] = $a[$j-1]; } $a[$j] = $d; } } } # Functions for other sorting algorithms ... ?>

The following diagram illustrates how this implementation works:

----------24 4 53 ... 42 | | | | +-----------+---+---+---+---+---+---+---+-------------------+ | | | / / / 5 ... 17 20 29 36 39 | | | | | | fromIndex j-1 j i toIndex-1

Note that:

- Elements to be sorted are stored from "fromIndex" to "toIndex-1" inclusive.
- At any given time, elements from "fromIndex" to "i-1" are sorted.
- At any given time, elements from "i" to "toIndex-1" are not sorted.
- As shown in the diagram, element at location "i" needs to be inserted at location "j", and all elements from "j" to "i-1" need to be shifted to the right by one location.

Here are the performance test results of insertionSort() function using PHP 5.6. The execution of insertionSort() is so slow. I have to reduce the number of tests to 10 to avoid long waiting time.

Array size: 10000 Average sorting time: 2212.8 milliseconds Number of tests: 10 Performance: 221.28 O(N) microseconds Performance: 16.652979360131 O(N*Log2(N)) microseconds Performance: 0.022128 O(N*N) microseconds Array size: 20000 Average sorting time: 9484.1 milliseconds Number of tests: 10 Performance: 474.205 O(N) microseconds Performance: 33.189707869452 O(N*Log2(N)) microseconds Performance: 0.02371025 O(N*N) microseconds Array size: 30000 Average sorting time: 23328.8 milliseconds Number of tests: 10 Performance: 777.62666666667 O(N) microseconds Performance: 52.285595760466 O(N*Log2(N)) microseconds Performance: 0.025920888888889 O(N*N) microseconds

The results showed us that the performance of insertion method is O(N*N), because the last performance line gave me a constant, when I increased the array size.

Here is the comparison of insertionSort() performance with 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

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