Mastering Sorting: Understanding and Implementing Insertion Sort in PHP

Mastering Sorting: Understanding and Implementing Insertion Sort in PHP
Photo by Line Kjær / Unsplash

Sorting algorithms are fundamental to computer science, enabling us to arrange data in a meaningful way. Among the various sorting techniques, insertion sort stands out for its simplicity and efficiency, particularly when dealing with small or partially sorted datasets. In this article, we will explore the intricacies of the insertion sort algorithm, including a practical implementation in PHP.

What is Insertion Sort?

Insertion sort is a comparison-based sorting algorithm that builds a sorted array one element at a time. The concept is akin to sorting playing cards in your hands: you take one card at a time and insert it into the correct position among the cards you’ve already sorted. This method makes insertion sort particularly intuitive and easy to implement.

How Does Insertion Sort Work?

The algorithm works by dividing the input array into a sorted and an unsorted section. Initially, the sorted section consists of just the first element. As the algorithm progresses, elements from the unsorted section are picked and inserted into the correct position within the sorted section.

The process is as follows:

  1. Starting from the second element of the array, the algorithm compares it with the elements in the sorted section.
  2. If the current element is smaller than the last element of the sorted section, it is swapped until it finds its correct position.
  3. This continues until all elements are processed, resulting in a completely sorted array.

Characteristics of Insertion Sort

One of the most compelling features of insertion sort is its adaptive nature. The algorithm performs efficiently when the input data is nearly sorted, with a best-case time complexity of $O(n)$. However, in the worst-case scenario—where the data is sorted in reverse order—it operates with a time complexity of $O(n^2)$. Despite this, insertion sort remains a stable sorting algorithm, preserving the relative order of equal elements, which can be crucial in certain applications.

Pseudocode

function insertionSort(A: Array of Element; n: Integer)
    for i := 1 to n - 1 do // Start from the second element (index 1)
        j := i
        while (j > 0) and (A[j - 1] > A[j]) do
            swap(A[j - 1], A[j]) // Swap elements to insert A[i] in the right position
            j := j - 1
        endwhile
    endfor
endfunction

A Practical Implementation in PHP

To help you understand how insertion sort can be implemented in a programming context, here’s a straightforward PHP implementation. This code will sort an array of integers in ascending order using the insertion sort technique.

<?php

/**
 * Sorts an array using the insertion sort algorithm.
 *
 * @param array $array The array to be sorted.
 * @return array The sorted array.
 */
function insertionSort(array $array): array {
    $n = count($array);
    
    for ($i = 1; $i < $n; $i++) { // Start from the second element (index 1)
        $j = $i;
        
        // Shift elements of $array[0..$i-1], that are greater than $array[$i],
        // to one position ahead of their current position
        while ($j > 0 && $array[$j - 1] > $array[$j]) {
            // Swap the elements
            $temp = $array[$j - 1];
            $array[$j - 1] = $array[$j];
            $array[$j] = $temp;
            $j--;
        }
    }
    
    return $array; // Return the sorted array
}

// Example usage
$inputArray = [5, 2, 9, 1, 5, 6];
$sortedArray = insertionSort($inputArray);

echo "Sorted Array: " . implode(", ", $sortedArray) . PHP_EOL;
?>

This code defines a function, insertionSort, that takes an array as input and sorts it in ascending order. The core logic revolves around the nested loops where comparisons and swaps occur, ensuring each element finds its rightful place. The example usage at the end demonstrates how to invoke the function and display the sorted array.

💡
Live demo in PHP and Go

When to Use Insertion Sort

Insertion sort shines in specific scenarios. If you are working with small datasets or if you need to sort data that is mostly sorted, this algorithm can be an excellent choice due to its low overhead and simplicity. Furthermore, its stability can be advantageous in applications where the order of equal elements matters.

Finally

While insertion sort may not be the fastest sorting algorithm available, its ease of understanding and implementation makes it a valuable tool in a programmer's toolkit. By mastering this algorithm, you will gain insight into the foundational concepts of sorting, which will serve you well as you tackle more complex algorithms in the future.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe