算法系列-插入排序

定义

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

原理

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

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'));

Donate

Buy me a coffee, if you thought this blog does help you. Click the link below.

My Personal PayPal.

21条评论

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

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Thank you for your entire labor on this web site. My mom take interest in participating in investigations and it is obvious why. My partner and i learn all regarding the lively tactic you produce simple things through your web site and in addition invigorate response from the others on the subject then our favorite princess is truly studying a whole lot. Take advantage of the rest of the year. You have been doing a superb job.

  7. I’m commenting to let you be aware of of the amazing discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such beneficial, dependable, edifying and cool guidance on that topic to Emily.

  8. I precisely desired to say thanks again. I’m not certain the things I could possibly have created in the absence of the actual creative concepts discussed by you concerning my concern. It absolutely was a depressing concern for me personally, but considering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating men and women by way of a web site. I am certain you’ve never come across all of us.

  9. My spouse and i got quite happy that John could deal with his preliminary research through the ideas he grabbed out of your web page. It’s not at all simplistic to just continually be giving out points that many others have been selling. And we also fully understand we have got the website owner to be grateful to for that. The main explanations you have made, the simple site menu, the friendships you can help foster – it’s got many impressive, and it’s really helping our son and the family imagine that the matter is excellent, and that is pretty indispensable. Thank you for everything!

  10. I happen to be writing to make you understand what a useful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make others without difficulty learn about specific tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trusted, informative as well as unique tips on your topic to Mary.

  11. I simply wanted to thank you so much once more. I do not know what I would’ve taken care of without these suggestions revealed by you over that subject. It was an absolute intimidating problem in my position, nevertheless taking a look at this specialized style you resolved the issue took me to leap with happiness. I will be happy for your service and then sincerely hope you realize what an amazing job you have been providing teaching some other people using your webblog. Most likely you haven’t got to know any of us.

  12. A lot of thanks for all your valuable hard work on this web page. Kate loves getting into investigation and it’s really easy to understand why. I know all of the dynamic medium you make insightful solutions on this web blog and as well as foster participation from people on that idea so our girl is certainly discovering a lot of things. Have fun with the remaining portion of the new year. You are conducting a powerful job.

  13. I precisely desired to say thanks again. I’m not certain the things I could possibly have accomplished in the absence of the advice contributed by you about my area. Certainly was a alarming case for me, but being able to see a expert approach you dealt with it forced me to cry for contentment. I’m just happier for this guidance and even hope you find out what a great job you are doing instructing other individuals through your blog post. I am sure you have never encountered all of us.

  14. I simply needed to thank you so much once again. I am not sure the things that I would’ve made to happen without the type of secrets provided by you on such a question. It seemed to be a very frustrating issue in my opinion, however , observing the very specialised strategy you processed that made me to leap over gladness. I am just happy for this information and in addition pray you know what a great job you happen to be getting into teaching people thru your web page. More than likely you have never got to know any of us.

  15. I am just writing to make you understand what a perfect experience my wife’s girl obtained using your webblog. She noticed so many pieces, which included what it is like to possess an excellent helping spirit to let others with no trouble learn about specific specialized things. You truly surpassed readers’ expected results. Thank you for providing these priceless, trusted, informative as well as fun tips about this topic to Lizeth.

  16. A lot of thanks for all your efforts on this site. Gloria enjoys engaging in investigation and it’s easy to understand why. Almost all know all concerning the dynamic means you deliver great items on the web blog and as well encourage contribution from other people on that concern plus our daughter is actually being taught a lot. Have fun with the remaining portion of the new year. You’re the one conducting a glorious job.

  17. I simply wanted to thank you very much once more. I do not know what I would have tried without these tactics shared by you regarding that subject matter. It was actually an absolute intimidating problem in my position, nevertheless taking a look at this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus sincerely hope you really know what an amazing job you have been putting in training the mediocre ones via your webblog. Most probably you haven’t met any of us.

  18. I precisely needed to appreciate you once again. I am not sure the things that I might have handled in the absence of the entire pointers provided by you on such a problem. It had been a very frightening difficulty in my circumstances, however , looking at the skilled manner you processed that made me to jump over fulfillment. I am happier for this help and in addition hope you know what a great job you are getting into instructing others through the use of your site. I know that you have never encountered all of us.

  19. Helpful info. Fortunate me I found your site accidentally, and I am surprised why this twist of fate did not came about in advance! I bookmarked it. https://stomachulcersmedi.com best prescription drugs for stomach ulcers

  20. I am continually invstigating online for tips that can benefit me. Thanks! https://hivmedi.com prep medications hiv

  21. wonderful post, very informative. I wonder why the other experts of this sector do not realize this. You should continue your writing. I am sure, you have a huge readers’ base already! https://anxietymedi.com medication for Anxiety

发表评论

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