Insertion Sort Sorting Algorithm Learning-Insertion Sort

WBOY
Release: 2016-07-28 08:29:55
Original
1164 people have browsed it

Learning of sorting algorithms, insertion sorting, and PHP code implementation.

Idea: Sort from small to large. Compare the newly inserted data with the previous one. If the newly inserted data is smaller than the previous one, swap the order.

<?php
//排序--插入
$data = array(10,9,8,7,6,5,4,3,2,1);

fun($data);

function fun($data){
    echo implode(&#39;,&#39;,$data).&#39;<br>';
    $count = count($data);
    for($i=1;$i<$count;$i++){
        echo &#39;第&#39;.($i+1).&#39;个数插入:<br>';
        for($j=$i;$j>0;$j--){
            if($data[$j]<$data[$j-1]){
                echo $data[$j].&#39;<==>'.$data[$j-1].'<br>';
                $temp = $data[$j-1];
                $data[$j-1] = $data[$j];
                $data[$j] = $temp;
                echo implode(',',$data).'<br>';
            }
        }
        //echo implode(',',$data).'<br>';
        echo '------------------------------<br>';
    }
}
Copy after login

The above introduces insertion sort and sorting algorithm learning-insertion sort, including the content of insertion sort. 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