Blogger Information
Blog 7
fans 0
comment 1
visits 5756
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组排序-选择排序
bokewinner
Original
749 people have browsed it

选择排序的描述:

  1. 假设有n个数,比较的趟数为n-1次

  2. 每一趟得到最大值

  3. 把最大值的位置与最后一项的位置交换

<?php
$arr = array(12,23,34,45,15,2,12);
$len = count($arr);
for($i = 0;$i < $len - 1;$i++)
{
    //最大值的初始化
    $max = $arr[0];
    $index = 0;
    //求最大值
    for($j = 0;$j < $len - $i;$i++)//-$i为减少之前比较好的数(已经固定好的数)
    {
        if($max < $arr[$j])
        {
            $index = $j;
            $max = $arr[$j];
        }
    }
    //交换最大值的位置和最后一项的位置
    $temp = $arr[$index];
    $arr[$index] = $arr[$len-$i-1];
    $arr[$len-$i-1] = $temp;
}
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post