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

Shell Sort - Implementation in Perl

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

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 Perl implementation of Heap Sort algorithm:

#- Sort_Function.pl #- Copyright (c) 2015 HerongYang.com. All Rights Reserved. #- sub shellSort { my ($a, $fromIndex, $toIndex) = @_; my $h = 1; while ($h<$toIndex-$fromIndex) { $h = 3*$h + 1; } while ($h>1) { $h = int($h/3); for (my $i=$fromIndex+$h; $i<$toIndex; $i++) { my $d = $a->[$fromIndex+$i]; my $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 ... #- End 1;

Here are the performance test results of heapSort() function using Perl 5.18

Array size: 10000 Average sorting time: 98.732958984375 milliseconds Number of tests: 10 Performance: 9.8732958984375 O(N) microseconds Performance: 0.743039555373961 O(N*Log2(N)) microseconds Performance: 0.00098732958984375 O(N*N) microseconds Array size: 20000 Average sorting time: 226.566186523437 milliseconds Number of tests: 10 Performance: 11.3283093261719 O(N) microseconds Performance: 0.792870756720055 O(N*Log2(N)) microseconds Performance: 0.000566415466308594 O(N*N) microseconds Array size: 30000 Average sorting time: 376.612548828125 milliseconds Number of tests: 10 Performance: 12.5537516276042 O(N) microseconds Performance: 0.844081628131157 O(N*Log2(N)) microseconds Performance: 0.000418458387586806 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 Perl sort() 11 22 36 171 Quicksort 20 46 75 Merge Sort 48 100 153 Heap Sort 92 200 315 Shell Sort 99 227 377 Insertion Sort 4125 16015 37098 Selection Sort 8054 31249 68985 Bubble Sort 19344 78360 177353

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