PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Written By
Views

Sorting Algorithms in PHP: the bubble sort

Sorting Algorithms in PHP: the bubble sort

In the bubble sort algorithm, we take an array, and we swap the position of two values if they are not in the correct order. In order to do that, we'll need to have two nested iterations on the array. Here you can find a first implementation:

<?php

$numbers = [4, 2, 9, 6, 23, 12, 1, 5];
$sortedNumbers = bubbleSort($numbers);

echo "Ordered array: ";
foreach ($sortedNumbers as $number) {
    echo $number . " ";
}




function bubbleSort(array $array) : array
{
    $array_size = count($array);
    //cycle over the array
    for($i = 0; $i < $array_size -1; $i++) {
        //loop through all the array and swap the items if thay are not in the corrected order
        for($j = 0; $j < $array_size - $i - 1; $j++) {
            if($array[$j] > $array[$j + 1]) {
                $temporary = $array[$j];
                $array[$j] = $array[$j+1];
                $array[$j+1] = $temporary;
            }
        }
    }

    return $array;
}

we can optimize it, for example if the array is already ordered, we can skip the next iterations:

function bubbleSort(array $array) : array
{
    
    $array_size = count($array);
    //cycle over the array
    for($i = 0; $i < $array_size -1; $i++) {
        $iterate_again = false;
        //loop through all the array and swap the items if thay are not in the corrected order
        for($j = 0; $j < $array_size - $i - 1; $j++) {
            if($array[$j] > $array[$j + 1]) {
                //if you need to perform a swap, then you'll need to perform another loop
                $iterate_again = true;
                $temporary = $array[$j];
                $array[$j] = $array[$j+1];
                $array[$j+1] = $temporary;
            }

            if (!$iterate_again)
                return $array;
        }
    }

    return $array;
}

lastly, since the last item will always be in the right position, we can reduce the superiorlimit of the sort:

function bubbleSort(array $array) : array
{
    
    $superior_limit = count($array);
    //cycle over the array
    for($i = 0; $i < $array_size -1; $i++) {
        $iterate_again = false;
        //loop through all the array and swap the items if thay are not in the corrected order
        for($j = 0; $j < $superior_limit - $i - 1; $j++) {
            if($array[$j] > $array[$j + 1]) {
                //if you need to perform a swap, then you'll need to perform another loop
                $iterate_again = true;
                $temporary = $array[$j];
                $array[$j] = $array[$j+1];
                $array[$j+1] = $temporary;
            }

            if (!$iterate_again)
                return $array;

            //reduce the superior limit of the loop:
            $superior_limit--;
        }
    }

    return $array;
}

Comments (0)

loading comments