Home  >  Article  >  Backend Development  >  PHP quicksort

PHP quicksort

WBOY
WBOYOriginal
2016-07-28 08:25:36999browse
<?php
function quickSort($left,$right,$sort_arr = null){
    static $arr;
    if(!empty($sort_arr)){
        $arr = $sort_arr;
    }

    if($left >= $right){
        return;
    }

    $mark_num = $arr[$left];
    $mark_k = $left;
    $i = $left+1;
    $j = $right;

    while($i != $j){
        //左移判断
        while($arr[$j] > $mark_num && $j > $i){
            $j--;
        }

        //右移判断
        while($arr[$i] <= $mark_num && $i < $j){
            $i++;
        }

        $tmp = $arr[$i];
        $arr[$i] = $arr[$j];
        $arr[$j] = $tmp;   
    }

    if($arr[$i] < $mark_num){
        $arr[$mark_k] = $arr[$i];
        $arr[$i] = $mark_num;
    }

    quickSort($left,$i-1);
    quickSort($i+1,$right);
    return $arr;
}

The above introduces the PHP quick sort method, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP error_reportingNext article:PHP error_reporting