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

Selection Sort - Implementation in PHP

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

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 PHP implementation of Selection Sort algorithm:

<?php #- Sort_Functions.php #- Copyright (c) 2015 HerongYang.com. All Rights Reserved. #- function selectionSort(&$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 ... ?>

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 lowest 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 PHP 5.6.

Array size: 10000 Average sorting time: 3580.2 milliseconds Number of tests: 10 Performance: 358.02 O(N) microseconds Performance: 26.943689761905 O(N*Log2(N)) microseconds Performance: 0.035802 O(N*N) microseconds Array size: 20000 Average sorting time: 14129.4 milliseconds Number of tests: 10 Performance: 706.47 O(N) microseconds Performance: 49.445984159871 O(N*Log2(N)) microseconds Performance: 0.0353235 O(N*N) microseconds Array size: 30000 Average sorting time: 33807.6 milliseconds Number of tests: 10 Performance: 1126.92 O(N) microseconds Performance: 75.771171566113 O(N*Log2(N)) microseconds Performance: 0.037564 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 Insertion Sort 2213 9484 23329 Selection Sort 3580 14129 33808

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