Implementing Fundamental Sorting Algorithms in PHP

To organize an unstructured set of integers, the following dataset serves as the target for sorting operations:

$target = [45, 22, 89, 12, 67, 34, 90, 5, 78, 41];

1. Bubble Sort Implementation

This algorithm iterates through the list repeatedly, swapping adjacent elements if they are in the wrong order. The process continues until no swaps are needed, indicating a sorted state.

function sortBubbleAscending($numbers) {
    $count = count($numbers);
    
    for ($outer = 0; $outer < $count - 1; $outer++) {
        for ($inner = 0; $inner < $count - 1 - $outer; $inner++) {
            if ($numbers[$inner] > $numbers[$inner + 1]) {
                $temp = $numbers[$inner];
                $numbers[$inner] = $numbers[$inner + 1];
                $numbers[$inner + 1] = $temp;
            }
        }
    }
    return $numbers;
}

2. Selection Sort Approach

Selection sort divides the input list into two parts: the sorted sub-list at the left end which is initially empty, and the unsorted sub-list at the right end. It selects the minimum element from the unsorted sublist and swaps it with the leftmost element.

function sortSelectAscending($values) {
    $length = count($values);

    for ($i = 0; $i < $length - 1; $i++) {
        $minIndex = $i;

        for ($j = $i + 1; $j < $length; $j++) {
            if ($values[$minIndex] > $values[$j]) {
                $minIndex = $j;
            }
        }

        if ($minIndex != $i) {
            $temp = $values[$i];
            $values[$i] = $values[$minIndex];
            $values[$minIndex] = $temp;
        }
    }
    return $values;
}

3. Insertion Sort Method

Similar to sorting playing cards, insertion sort builds the final sorted array one item at a time. It takes an element from the input, finds its correct position among the previously sorted elements, and shifts larger elements up to make space.

function sortInsertAscending($data) {
    $size = count($data);

    for ($i = 1; $i < $size; $i++) {
        $currentValue = $data[$i];
        $position = $i - 1;

        while ($position >= 0 && $data[$position] > $currentValue) {
            $data[$position + 1] = $data[$position];
            $position--;
        }
        $data[$position + 1] = $currentValue;
    }
    return $data;
}

4. Quick Sort Procedure

Quick Sort uses a divide-and-conquer strategy. A "pivot" element is selected, and elements smaller than the pivot are moved to its left, while larger elements are moved to its right. The process is then recursively applied to the sub-arrays.

function sortQuickAscending($input) {
    if (!is_array($input)) {
        return false;
    }

    $count = count($input);
    if ($count <= 1) {
        return $input;
    }

    $pivot = $input[0];
    $leftPart = [];
    $rightPart = [];

    for ($k = 1; $k < $count; $k++) {
        if ($input[$k] > $pivot) {
            $rightPart[] = $input[$k];
        } else {
            $leftPart[] = $input[$k];
        }
    }

    $leftPart = sortQuickAscending($leftPart);
    $rightPart = sortQuickAscending($rightPart);

    return array_merge($leftPart, [$pivot], $rightPart);
}

Tags: PHP sorting-algorithms bubble-sort selection-sort insertion-sort

Posted on Thu, 14 May 2026 09:47:31 +0000 by greenie2600