Home > Backend Development > PHP Tutorial > PHP兑现各种排序

PHP兑现各种排序

WBOY
Release: 2016-06-13 13:16:48
Original
772 people have browsed it

PHP实现各种排序

<?php /**
 * 各种排序
 * @author zhaojaingwei
 * @since 2011/11/21 16:14
 *
 */

$list = array(3,5,1,2,10,8,15,19,20);

//快排
function fast(&$list, $low, $high){
    if($high - $low > 5){
        while($low  $temp){
            $high --;
        }

        $list[$low] = $list[$high];

        while($low  0; $i --){
        swap($list, $i, 0);
        heapfy($list, 0, $i - 1); 
    }
}

//创建堆
function buildHeap(&$list){
    for($i = (count($list) - 2)/2; $i >= 0; $i --){
         heapfy($list, $i, count($list) - 1);  
    }
}

//维护堆
function heapfy(&$list, $low, $high){
    $temp = $list[$low];

    for($i = ($low * 2 + 1); $i = 1){
        for($i = $code; $i = 0 && $list[$j] > $temp; $j -= $code){
                    $list[$j + $code] = $list[$j];
                    $a ++; 
                }
                $list[$j + $code] = $temp;
            }
        }

        $code = $code/3;
    }

    echo $a;
}

//直接插入排序
function insert(&$list){
    $a = 0;
    for($i = 1; $i  $temp; $j --){
                $a ++;
                $list[$j + 1] = $list[$j]; 
            }
            
            $list[$j + 1] = $temp;
        }
    }

    echo $a;
}

//简单选择排序
function select(&$list){
    $a = 0;
    for($i = 0; $i = $i; $j --){
            $a ++;
            if($list[$j] > $list[$j + 1]){
                $swap = TRUE;
                swap($list, $j, $j + 1);
            }
        }
    }
    echo $a;
}

//移动或交换函数
function swap(&$list, $i, $j){
    $temp = $list[$i];
    $list[$i] = $list[$j];
    $list[$j] = $temp;
}

?>
Copy after login
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