# PHP Program - Selection Sort

Selection sort is based on the idea of finding smallest or largest element in unsorted array and placing it at the correct position in the sorted array. This will result into increasing the length of the sorted array by one and decreasing the length of unsorted array by one after each iteration.

### Example:

To understand the selection sort, lets consider an unsorted array *[1, 10, 23, -2]* and discuss each step taken to sort the array in ascending order. In every pass, smallest element is found in the unsorted array and swapped with first element of unsorted array.

** First Pass:** The whole array is unsorted array and

*(-2)*is the smallest number in the array. After finding

*(-2)*as smallest number, it is swapped with first element of the array.

** Second Pass:** In this example, the left hand side array is sorted array and length of this array increase by one after each iteration. After first pass length of the sorted array is one. Rest of the array is unsorted array.

*(1)*is the smallest number in the unsorted array which is swapped with first element of the unsorted array. After this swap, length of the sorted array and unsorted array will be two.

__Third Pass__:*(10)* is the smallest number in the unsorted array and swapped with the first element of the unsorted array.

__Fourth Pass__:*(23)* is the only number in the unsorted array and found to be at the correct position.

## Implementation of Selection Sort

<?php // function for selection sort function selectionsort(&$Array, $n) { for($i=0; $i<$n; $i++) { $min_idx = $i; for($j=$i+1; $j<$n; $j++) { if($Array[$j] < $Array[$min_idx]) {$min_idx = $j;} } $temp = $Array[$min_idx]; $Array[$min_idx] = $Array[$i]; $Array[$i] = $temp; } } // function to print array function PrintArray($Array, $n) { for ($i = 0; $i < $n; $i++) echo $Array[$i]." "; echo "\n"; } // test the code $MyArray = array(1, 10, 23, 50, 4, 9, -4); $n = sizeof($MyArray); echo "Original Array\n"; PrintArray($MyArray, $n); selectionsort($MyArray, $n); echo "\nSorted Array\n"; PrintArray($MyArray, $n); ?>

The above code will give the following output:

Original Array 1 10 23 50 4 9 -4 Sorted Array -4 1 4 9 10 23 50

### Time Complexity:

The time complexity of selection sort is *Θ(N²)* in all cases even if the whole array is sorted because the entire array need to be iterated for every element and it contains two nested loops.

### Recommended Pages

- PHP - Swap two numbers
- PHP Program - Fibonacci Sequence
- PHP Program - Insertion Sort
- PHP Program - Find Factorial of a Number
- PHP Program - Find HCF of Two Numbers
- PHP Program - Merge Sort
- PHP Program - Shell Sort
- Stack in PHP
- Queue in PHP
- PHP Program - Find LCM of Two Numbers
- PHP Program - To Check Whether a Number is Palindrome or Not
- PHP Program - To Check Whether a String is Palindrome or Not
- PHP Program - Heap Sort
- PHP Program - Quick Sort
- PHP - Swap Two Numbers without using Temporary Variable
- PHP Program - To Check Armstrong Number
- PHP Program - Counting Sort
- PHP Program - Radix Sort
- PHP Program - Find Largest Number among Three Numbers
- PHP Program - Print Floyd's Triangle