Selection Sort - Implementation in Perl

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

Selection Sort is a simple and slow sorting algorithm that repeatedly selects the lowest or highest element from the un-sorted section and moves it to the end of the sorted section.

The basic idea of Selection 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. Assuming the sorting order is from low to high, find the element with the lowest comparable order from the un-sorted section.

3. Place the found element to the end of the sorted section.

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

The idea of selection sort comes from our daily life experiences. For example, at the end of card game, if you want to sort all the cards by rank and suit, you would put all the cards on the table, face up, find the lowest rank card, add it to the sorted piles, one pile per suit.

Here is my Perl implementation of Selection Sort algorithm:

```#- Sort_Function.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#-
sub selectionSort {
my (\$a, \$fromIndex, \$toIndex) = @_;
for (\$i=\$fromIndex; \$i<\$toIndex; \$i++) {
\$k = \$i;
for (\$j=\$i+1; \$j<\$toIndex; \$j++) {
if ((\$a->[\$k])>\$a->[\$j]) {
\$k = \$j;
}
}
\$d = \$a->[\$i];
\$a->[\$i] = \$a->[\$k];
\$a->[\$k] = \$d;
}
}

# Functions for other sorting algorithms ...

#- End
1;
```

The following diagram illustrates how this implementation works:

```                                -----------29
|  36  39      67   ...            42
|   |   |       |                   |
+-----------+---+---+---+---+---+---+---+-------------------+
|           |   |   |               |
4   ...    17  20  24  53-----------
|                   |   |           |                       |
fromIndex                 i-1   i           k              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, the element with the lowerest comparable order is located at "k", which will be placed at the end of the sorted section by swapping it with the element at location "i".

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

```Array size: 10000
Average sorting time: 8053.67790527344 milliseconds
Number of tests: 10
Performance: 805.367790527344 O(N) microseconds
Performance: 60.6099656225891 O(N*Log2(N)) microseconds
Performance: 0.0805367790527344 O(N*N) microseconds

Array size: 20000
Average sorting time: 31248.7899414062 milliseconds
Number of tests: 10
Performance: 1562.43949707031 O(N) microseconds
Performance: 109.355469620644 O(N*Log2(N)) microseconds
Performance: 0.0781219748535156 O(N*N) microseconds

Array size: 30000
Average sorting time: 68985.4992675781 milliseconds
Number of tests: 1
Performance: 2299.5166422526 O(N) microseconds
Performance: 154.613521828749 O(N*Log2(N)) microseconds
Performance: 0.0766505547417535 O(N*N) microseconds
```

The results showed 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 selectionSort() performance with other sorting functions. As you can see, Selection Sort is much slower than Insertion Sort.

```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
Selection Sort     8054   31249   68985
```

Table of Contents