Shell Sort function in PHP

Discussion in 'PHP' started by pradeep, Aug 23, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Shell Sort function in PHP

    PHP:
    <?
     function 
    shellsort($elements,$length)
     {
         
    $k=0;
         
    $gap[0]=(int) ($length 2);
         while(
    $gap[$k]>1)
         {
             
    $k++;
             
    $gap[$k]=(int)($gap[$k-1]/2);
         }
    //end while
     
         
    for($i=0;$i<=$k;$i++)
         {
         
    $step=$gap[$i];
             for(
    $j=$step;$j<$length;$j++)
             {
                 
    $temp=$elements[$j];
                 
    $p=$j-$step;
                 while(
    $p>=&& $temp<$elements[$p])
                 {
                     
    $elements[$p+$step]=$elements[$p];
                     
    $p=$p-$step;
                 }
    //end while
                 
    $elements[$p+$step]=$temp;
             }
    //endfor j
         
    }//endfor i
         
    return $elements;
     }
    // end function
     
     // Exmaple
     // $SortedElements=shellsort($UnsortedElements,length of list(an integer));
     // e.g: $elements=shellsort($elements,10);
     
    ?>
     

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