// you’re reading...

Apache / PHP / MySQL

PHP Quicksort Demonstration

php-med-transWhile looking for a hip-pocket sorting, algorithm, I found some sample code here and thought it was perfect the way it was.

function quicksort($seq) {
    if(!count($seq)) return $seq;
    $pivot= $seq[0];
    $low = $high = array();
    $length = count($seq);
    for($i=1; $i < $length; $i++) {
        if($seq[$i] <= $pivot) {
            $low [] = $seq[$i];
        } else {
            $high[] = $seq[$i];
        }
    }
    return array_merge(quicksort($low), array($pivot), quicksort($high));
}

So here’s a working example of the above code.

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • TwitThis

Discussion

No comments for “PHP Quicksort Demonstration”

Post a comment

You must be logged in to post a comment.

 

December 2009
S M T W T F S
« Sep   Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031  
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • TwitThis