Bubble Sort in PHP

Discussion in 'PHP' started by pein87, Feb 1, 2012.

  1. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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 = [URL="http://www.php.net/is_array"]is_array[/URL]( $args )? [URL="http://www.php.net/count"]count[/URL]( $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( [URL="http://www.php.net/strtolower"]strtolower[/URL]( $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 ( [URL="http://www.php.net/strtolower"]strtolower[/URL]( $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 = [URL="http://www.php.net/array"]array[/URL](1,8,7,4,8,12,34,56);
    
    $na = bubbleSort($a,"asc");
    
    [URL="http://www.php.net/print_r"]print_r[/URL]($na);
    
    ?>
    

    References



    Active Tuts Plus
     
    shabbir likes this.
  2. parker1234

    parker1234 Banned

    Joined:
    Jan 20, 2012
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Nice code.........for bubble sort
     
  3. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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.
     
  4. tinytomhanks

    tinytomhanks Banned

    Joined:
    Apr 20, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    business admin
    Location:
    US and India
    Home Page:
    http://www.itchimes.com/
    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.
     
  5. The Alchemist

    The Alchemist New Member

    Joined:
    Jul 7, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    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 :
    After the first check, the array becomes :
    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...
     
  6. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice