Fundamentals 4 min read

Common Sorting Algorithms in PHP: Bubble Sort, Selection Sort, Insertion Sort, and Quick Sort

This article introduces four classic sorting algorithms—bubble sort, selection sort, insertion sort, and quick sort—explaining their principles and providing complete PHP implementations for each to help readers understand and apply these fundamental techniques.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Common Sorting Algorithms in PHP: Bubble Sort, Selection Sort, Insertion Sort, and Quick Sort

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

function bubbleSort($arr) {
    $len = count($arr);
    for ($i = 1; $i < $len; $i++) {
        for ($k = 0; $k < $len - $i; $k++) {
            if ($arr[$k] > $arr[$k + 1]) {
                $tmp = $arr[$k + 1];
                $arr[$k + 1] = $arr[$k];
                $arr[$k] = $tmp;
            }
        }
    }
    return $arr;
}
$arr = array(1, 43, 54, 62, 21, 66, 32, 78, 36, 76, 39);
print_r($arr);

Selection sort works by repeatedly finding the minimum element from the unsorted portion and moving it to the beginning of the list.

function select_sort($arr) {
    for ($i = 0, $len = count($arr); $i < $len - 1; $i++) {
        $p = $i;
        for ($j = $i + 1; $j < $len; $j++) {
            if ($arr[$p] > $arr[$j]) {
                $p = $j;
            }
        }
        if ($p != $i) {
            $tmp = $arr[$p];
            $arr[$p] = $arr[$i];
            $arr[$i] = $tmp;
        }
    }
    return $arr;
}
$arr = array(1, 43, 54, 62, 21, 66, 32, 78, 36, 76, 39);
print_r($arr);

Insertion sort builds a sorted sequence one element at a time by taking each unsorted element and inserting it into its correct position within the already sorted part.

function insert_sort($arr) {
    $len = count($arr);
    for ($i = 1; $i < $len; $i++) {
        $tmp = $arr[$i];
        for ($j = $i - 1; $j >= 0; $j--) {
            if ($tmp < $arr[$j]) {
                $arr[$j + 1] = $arr[$j];
                $arr[$j] = $tmp;
            } else {
                break;
            }
        }
    }
    return $arr;
}
$arr = array(1, 43, 54, 62, 21, 66, 32, 78, 36, 76, 39);
print_r($arr);

Quick sort is a divide‑and‑conquer algorithm that selects a pivot element, partitions the remaining elements into those less than and greater than the pivot, and then recursively sorts the sub‑arrays, achieving average‑case O(n log n) performance.

function quick_sort($arr) {
    if (!is_array($arr)) {
        return false;
    }
    $length = count($arr);
    if ($length <= 1) {
        return $arr;
    }
    $left = $right = array();
    for ($i = 1; $i < $length; $i++) {
        if ($arr[$i] < $arr[0]) {
            $left[] = $arr[$i];
        } else {
            $right[] = $arr[$i];
        }
    }
    $left = quick_sort($left);
    $right = quick_sort($right);
    return array_merge($left, array($arr[0]), $right);
}
$arr = array(1, 43, 54, 62, 21, 66, 32, 78, 36, 76, 39);
print_r($arr);
algorithmPHPsortingbubble sortquick sort
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.