Bubble Sort in PHP

pein87's Avatar author of Bubble Sort in PHP
This is an article on Bubble Sort in PHP in PHP.
Bubble sort in this form allows someone to order number arrays from lowest number to highest number and vice verse. This article serves the purpose of providing the bubble sort algorithm for use in php. I found a tuts @ active.tutsplus and I used it as a base to create a direct port over to php.

The Code



This code will sort a numbered list in order based on the values

Code: php
Code:
<?php
function bubbleSort( $args, $type )
{
    /* variables for looping */
    $i = 0;
    $k = 0;
    
    /* error types 0 = not array; 1 = not ASC or DES */
    $err[0] = "Error: Input is not an array."; // code 0
    $err[1] = "Error: Type is not ASC or DES."; // code 1
    
    /* temp variable to hold data */
    $temp;
    
    /* checks if the args variable is an array and if it is get its count if its not set it to 0 */
    $c = is_array( $args )? count( $args ) : 0; 
    
    /* checks if $c is set to 0 to trigger error; code 0 */
    if($c == 0)
    {
        
        return $err[0];
        
    }
    
    /* checks $type to see if it matches what we want it to, ASC is expected */
    if( strtolower( $type ) == "asc" )
    {
        /* initial counter that is used for the second loops < or > step */
        for( $i = 0; $i < $c; $i++ )
        {
            /* uses first loops count to perform operations on array */
            for( $k = $c - 1;$k > $i; $k-- )
            {
                /* compares values and performs array shifts */
                if( $args[$k - 1] > $args[$k] )
                {
                    
                    $temp = $args[$k - 1];
                    $args[$k - 1] = $args[$k];
                    $args[$k] = $temp;
                    
                }
                
            }
            
        }
    }
    /* checks $type to see if it matches what we want it to, DES is expected */
    else if ( strtolower( $type ) == "des" )
    {
        /* initial counter that is used for the second loops < or > step */
        for( $i = 0; $i < $c; $i++ )
        {
            /* uses first loops count to perform operations on array */
            for( $k = $c - 1;$k > $i; $k-- )
            {
                /* compares values and performs array shifts */
                if( $args[$k - 1] < $args[$k] )
                {
                    
                    $temp = $args[$k - 1];
                    $args[$k - 1] = $args[$k];
                    $args[$k] = $temp;
                    
                }
                
            }
            
        }
    }
    /* $type does not equal set standards trigger error: code 1 */
    else
    {
        return $err[1];
    }
    /* return shifted array */
    return $args;
}

$a = array(1,8,7,4,8,12,34,56);

$na = bubbleSort($a,"asc");

print_r($na);

?>

References



Active Tuts Plus
shabbir like this
parker1234's Avatar
Banned
Nice code.........for bubble sort
pein87's Avatar
Ambitious contributor
I can't take any credit as it's not originally my code. It is a port over from action script. Not a perfect copy since the author unnecessarily used memory for additional variables when he could have replaced the values of the two needed. That and error codes.
tinytomhanks's Avatar
Banned
Nice effort to implement, I think it is most popular sorting technique. The process is simple, you keep comparing pairs of adjacent elements of the sequence, if the they are in the wrong order swap them and do this till there are no swapping to do.
The Alchemist's Avatar, Join Date: Jul 2012
Go4Expert Member
Nice code for bubble sort technique... Just one more thing... Make it a bit advanced by keeping a check after every cycle so that, after the array is sorted, the loop doesnt run...
For example if this is the array :
Quote:
10 9 100 200 300 400
After the first check, the array becomes :
Quote:
9 10 100 200 300 400
Which does not need running the loop any further...

But, in your code, the loop runs further... Increasing the complexity of the program and making the code slower...

This is just an advice... But your code is well implemented...
pein87's Avatar
Ambitious contributor
I'd still need to loop through the rest of the array, and compare the next number to make sure that its less than the next number. That would take more time than to dumbly sort the list.