Open In App

PHP Program for Insertion Sort

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Insertion sort is a simple and efficient sorting algorithm that builds the final sorted array one element at a time. Insertion sort provides several advantages such as simple implementation, efficient for small data sets, and more efficient in practice than most other simple quadratic algorithms like selection sort or bubble sort.

Working of Insertion Sort

Insertion Sort Algorithm

  • To sort an array of size N in ascending order, iterate over the array.
  • For each element (referred to as the “key”), compare it to its preceding element.
  • If the key element is smaller than its predecessor, continue comparing it to the preceding elements.
  • Shift the larger elements one position forward to make room for the key element.
  • Repeat the process until the array is fully sorted.

Example: The below mention code implements the Insertion Sort Algorithm in PHP.

PHP
<?php 

function insertionSort(array &$arr) {
    $n = count($arr);
    for ($i = 1; $i < $n; $i++) {
        $key = $arr[$i];
        $j = $i - 1;

        while ($j >= 0 && $arr[$j] > $key) {
            $arr[$j + 1] = $arr[$j];
            $j--;
        }
        
        $arr[$j + 1] = $key;
    }
}

function printArray(array $arr) {
    foreach ($arr as $value) {
        echo $value . " ";
    }
    
    echo "\n";
}

// Driver Code
$arr = [12, 11, 13, 5, 6];

insertionSort($arr);
printArray($arr);

?>

Output
5 6 11 12 13 

Time Complexity of Insertion Sort

  • The worst-case time complexity of the Insertion sort is O(N^2).
  • The average-case time complexity of the Insertion sort is O(N^2).
  • The best-case time complexity of the best case is O(N).

Similar Reads

Javascript Program For Insertion Sort In A Singly Linked List
We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert current node in sorted way in sorted or result list. 3
3 min read
JavaScript Program for Insertion Sort
What is Insertion Sort Algorithm?Insertion sorting is one of the sorting techniques that is based on iterating the array and finding the right position for every element. It compares the current element to the predecessors, if the element is small compare it with the elements before. Move ahead to all greater elements making space for the swapped e
4 min read
Insertion Sort Visualization using JavaScript
Insertion sort is a simple sorting algorithm in which values from the unsorted part are picked and placed at the correct position in the sorted part. In order to know more about it. Please refer Insertion Sort. An algorithm like Insertion Sort can be easily understood by visualizing instead of long codes. In this article, Insertion Sort Visualizer
5 min read
Php Program to Check if it is possible to sort the array after rotating it
Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of the array to the front of the array to sort it.A =
3 min read
Php Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:   Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20   Approach: Following are the steps:  Sort e
3 min read
Php Program For Counting Inversions In An Array - Set 1 (Using Merge Sort)
Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i Example:  Input: arr[] =
2 min read
PHP Program for Selection Sort
This article will show you how to sort the array elements using the Selection sort in PHP. Selection SortSelection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion. Selection Sort is a simple sorting algorit
2 min read
PHP Program for Bubble Sort
This article will show you how to sort array elements using Bubble Sort in PHP. Bubble Sort is a simple sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. The time complexity of this algorithm is quite high. Bubble Sort AlgorithmTraverse the array from the left and compare the adjacent elements
2 min read
PHP Program to Sort Names in an Alphabetical Order
Sorting names in alphabetical order is a common task in programming. PHP provides several functions and methods to achieve this. Examples: Input: arr = ["Sourabh", "Anoop, "Harsh", "Alok", "Tanuj"]Output: ["Alok", "Anoop", "Harsh", "Sourabh", "Tanuj"]Input: arr = ["John", "Alice", "Bob", "Eve", "Charlie"] Output: ["Alice", "Bob", "Charlie", "Eve",
2 min read
PHP Program to Sort an Array Elements in Descending Order
Given an array containing some elements, the task is to sort the array elements in descending order in PHP. Sorting elements of an array is a common operation in programming, and PHP provides several methods to accomplish this task. Table of Content Using rsort() FunctionUsing array_reverse() with sort() FunctionsUsing usort() with Custom Compariso
2 min read