1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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