PHP quicksort

WBOY
Release: 2016-07-28 08:25:36
Original
1017 people have browsed it
<?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;
}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template