算法系列–冒泡排序

定义

冒泡排序是用来排列一连串数字的算法

原理

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
  3. 针对所有的元素重复以上的步骤,除了最后一个。
  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

PHP实现

PHP沙盒环境

拷贝代码到沙盒环境可以看到效果

//PHP实现升序
function bubble_asort(array $numbers = []){
  //获取数组元素个素总数 $n
  $n = count($numbers);
  //控制循环次数 $n-1
  for($j= 1; $j< $n; $j++){
   //控制元素坐标 $n-$j
    for($k = 0; $k < $n-$j; $k++){
      if($numbers[$k] > $numbers[$k + 1]){
        $temp = $numbers[$k];
        $numbers[$k] = $numbers[$k+1];
        //元素值大的往后排
        $numbers[$k+1] = $temp;
      }
    }
  }
  return $numbers;
}

//PHP实现降序
function bubble_dsort(array $numbers = []){
  //获取数组元素个素总数 $n
  $n = count($numbers);
  //控制循环次数 $n-1
  for($j= 1; $j< $n; $j++){
    //控制元素坐标 $n-$j
    for($k = 0; $k < $n-$j; $k++){
      if($numbers[$k] < $numbers[$k + 1]){
        $temp = $numbers[$k];
        $numbers[$k] = $numbers[$k+1];
        //元素值小的往后排
        $numbers[$k+1] = $temp;
      }

    }
  }
  return $numbers;
}

//例子
$numbers = [1,3,4,22,88,33,31,42];
print_r(bubble_asort($numbers));
print_r(bubble_dsort($numbers));

//PHP内置函数实现
sort($numbers,1);
print_r($numbers);

rsort($numbers,1);
print_r($numbers);

24 comments

  1. Perfect piece of work you have done, this internet site is really cool with fantastic information.

  2. I’d always want to be update on new blog posts on this site, saved to bookmarks!

  3. I happen to be writing to make you understand what a terrific experience our princess undergone visiting yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an incredible teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

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

  5. I’m commenting to let you be aware of of the awesome 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 effective, dependable, edifying and cool guidance on that topic to Emily.

  6. I intended to compose you a bit of note in order to give many thanks again considering the beautiful concepts you’ve contributed above. It’s extremely generous of people like you in giving easily all a few people could have advertised as an e-book in making some bucks for their own end, certainly considering that you could have done it if you considered necessary. The concepts additionally acted as a easy way to be certain that many people have a similar desire just as mine to figure out a great deal more concerning this condition. Certainly there are a lot more enjoyable instances ahead for folks who discover your blog.

  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 am only writing to make you understand what a really good experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trustworthy, revealing as well as unique tips on your topic to Mary.

  9. I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey both interesting and useful guidelines by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a dazzling job.

  10. I enjoy you because of all of your effort on this blog. Debby delights in conducting internet research and it’s easy to see why. A number of us hear all about the compelling manner you create effective guides by means of the blog and boost contribution from other ones about this article and my child has been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a fabulous job.

  11. I precisely had to appreciate you all over again. I am not sure the things that I might have followed in the absence of the entire information documented by you directly on such industry. It had become a real distressing crisis in my circumstances, however , encountering the skilled form you managed that made me to jump over fulfillment. 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.

  12. My husband and i felt absolutely ecstatic Edward could complete his inquiry from the ideas he discovered from your very own site. It’s not at all simplistic just to be giving for free helpful tips that men and women could have been making money from. And we do know we have the blog owner to appreciate because of that. Most of the explanations you’ve made, the easy blog menu, the friendships you assist to engender – it’s everything excellent, and it’s assisting our son and our family do think that content is cool, and that’s extremely fundamental. Many thanks for all the pieces!

  13. I simply wished to thank you very much yet again. I do not know what I would have undertaken without those tips shared by you regarding this theme. Previously it was the terrifying setting in my view, nevertheless understanding your well-written technique you solved the issue took me to weep with joy. Now i’m thankful for your support and thus trust you really know what an amazing job you were putting in training the others via your website. Most probably you haven’t met any of us.

  14. I am also commenting to let you know of the helpful encounter my friend’s daughter found reading through your web page. She figured out numerous issues, not to mention what it’s like to have an amazing giving mood to have most people really easily know selected impossible subject areas. You really exceeded our own expectations. Many thanks for imparting those useful, trustworthy, revealing as well as unique tips on your topic to Sandra.

  15. Hello. fantastic job. I did not expect this. This is a fantastic story. Thanks! https://hairlossbimedi.com hair loss medications

  16. Great info and straight to the point. I am not sure if this is in fact the best place to ask but do you folks have any thoughts on where to get some professional writers? Thanks 🙂 https://livermedi.com what to take for liver pain

  17. You got a very good website, Glad I found it through google. https://glaucomamedi.com glaucoma medications

  18. I am continually invstigating online for tips that can assist me. Thanks! https://myastheniamedi.com new medication for myasthenia gravis

  19. I am always searching online for articles that can help me. Thank you! https://myastheniamedi.com treatment of myasthenia gravis

  20. You got a very good website, Glad I found it through google. https://atherosclerosismed.com medication for atherosclerosis

  21. Wonderful web site. Lots of useful information here. I’m sending it to a few buddies ans also sharing in delicious. And obviously, thank you on your effort! https://parkinsonmedi.com parkinson medications

  22. 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://epilepsymedi.com best epilepsy drugs

  23. You got a very fantastic website, Gladiolus I observed it through google. https://heartischemiamed.com heart ischemia medication

Leave a comment

Your email address will not be published. Required fields are marked *