**Sorting Algorithm Tutorials - Herong's Tutorial Examples** - v6.10, by Dr. Herong Yang

Insertion Sort - Implementation in Perl

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

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 Perl implementation of the Insertion Sort algorithm in the Sort_Functions.pl script. The binary search method was used to find the target of the insertiong in the sorted section.

#- Sort_Function.pl #- Copyright (c) HerongYang.com. All Rights Reserved. #- sub insertionSort { my ($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 = int(($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 ... #- End 1;

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 Perl 5.18. 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: 4124.81560058594 milliseconds Number of tests: 10 Performance: 412.481560058594 O(N) microseconds Performance: 31.0423305589777 O(N*Log2(N)) microseconds Performance: 0.0412481560058594 O(N*N) microseconds Array size: 20000 Average sorting time: 16015.9927734375 milliseconds Number of tests: 10 Performance: 800.799638671875 O(N) microseconds Performance: 56.0481354466577 O(N*Log2(N)) microseconds Performance: 0.0400399819335937 O(N*N) microseconds Array size: 30000 Average sorting time: 37097.9611083984 milliseconds Number of tests: 10 Performance: 1236.59870361328 O(N) microseconds Performance: 83.1456825062246 O(N*Log2(N)) microseconds Performance: 0.0412199567871094 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 Perl sort() 11 22 36 171 Insertion Sort 4125 16015 37098

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

►Sorting Algorithms Implementations in Perl

Sort_Test.pl - Sorting Performance Test

►Insertion Sort - Implementation in Perl

Selection Sort - Implementation in Perl

Bubble Sort - Implementation in Perl

Quicksort - Implementation in Perl

Merge Sort - Implementation in Perl

Heap Sort - Implementation in Perl

Shell Sort - Implementation in Perl

Sorting Algorithms Implementations in Python