算法系列-插入排序

phpman.song@gmail.com发布

定义

插入排序是用来排列一连串数字的算法

原理

从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。

PHP实现

PHP沙盒环境

function insertion_sort(array $numbers=[], string $sort='asc'){
  $n = count($numbers);
  for($i =1;$i<$n;$i++){
    $index = $i-1;
    $temp = $numbers[$i];
    if($sort == 'asc'){
       while($index>=0 && $numbers[$index] > $temp){
         $numbers[$index + 1] = $numbers[$index];
         $index--;
       }
      $numbers[$index+1] = $temp;
    }elseif($sort=='desc'){
       while($index>=0 && $numbers[$index] < $temp){
         $numbers[$index + 1] = $numbers[$index];
         $index--;
       }
       $numbers[$index+1] = $temp;
    }
    
  }
  return $numbers;  
}

//例子
$numbers = [1,3,4,22,88,33,31,42];
print_r(insertion_sort($numbers));
print_r(insertion_sort($numbers,'desc'));
分类: 算法

18 条评论

sprzedam samochód · 2021年1月5日 上午6:28

Bardzo podoba mi się twoja strona. Dziękuję za świetne informacje i zapraszam na stronę o samochodach. Do zobaczenia!

loan · 2021年1月7日 上午8:42

I am just writing to make you understand what a outstanding experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some specialized things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

viagra online · 2021年1月7日 下午8:20

Thanks for your own labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful way you produce valuable tips via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

viagra buy · 2021年1月7日 下午9:50

I wanted to send you the very little remark to finally thank you the moment again over the remarkable tactics you have shared on this page. This has been simply surprisingly open-handed with you to offer unhampered what most of us would’ve offered for sale for an ebook to help make some money on their own, precisely seeing that you might have tried it in case you desired. Those suggestions in addition worked to become good way to realize that some people have the identical keenness similar to my personal own to realize significantly more when it comes to this matter. I believe there are several more pleasant sessions up front for people who scan your site.

levitra tablets · 2021年1月8日 上午5:38

Needed to write you this very small word to help thank you very much yet again with your wonderful views you have shown on this website. This is so wonderfully open-handed with you to supply without restraint what exactly some people would have supplied for an electronic book to make some profit on their own, specifically since you might well have tried it in the event you wanted. Those tricks likewise worked to provide a great way to understand that the rest have the same dreams like my own to grasp good deal more on the subject of this issue. I’m sure there are many more fun occasions ahead for individuals that look into your blog post.

发表评论

邮箱地址不会被公开。 必填项已用*标注