In this tutorial, we will learn a simple sorting algorithm - Selection Sort.
Table Of Content
1. Problem to Solve
Given a list of numbers as shown below, please sort them in ascending order.
- You are required to use Selection Sort algorithm when sorting the numbers.
- You are required to implement the algorithm in PHP language.
Selection Sort works by maintaining a sorted sub-list, finding the smallest item from the master list and swap it to the last element of the sub-list until all items are sorted.
The sorted sub-list's length is increased by one, whereas the master list's length is shrunk by one after each swap.
Pseudocode of Selection Sort algorithm can be written as follows:
3. PHP Implementation
We need an outer FOR loop to iterate through the master list and an inner FOR loop to find the smallest item from the master list.
Take note that the inner loop starts from i.
4. The End
If you like our post, please follow us on Twitter
and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.