算法系列-选择排序

定义

选择排序是用来排列一连串数字的算法

原理

  1. 第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置
  2. 再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾
  3. 重复第二步,直到全部待排序的数据元素的个数为零

PHP实现

PHP沙盒环境

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

//选择排序升序
function selection_sort(array $numbers=[], string $order = 'asc'){
   //获取数组元素个素总数 $n
   $n = count($numbers);
   for($i = 0; $i< $n-1; $i++){
     $index = $i;    
     for($j=$i+1;$j<$n;$j++){
  
        if($order == 'asc'){
           if($numbers[$j] < $numbers[$index]){
             //获取最小元素坐标
             $index = $j;
           }
        }elseif($order='desc'){
           if($numbers[$j] > $numbers[$index]){
             //获取最大元素坐标
             $index = $j;
           }
        }
       
     }
     //将i元素和最小元素或者最大元素交换
     if($i != $index){
       $temp = $numbers[$i];
       $numbers[$i] = $numbers[$index];
       $numbers[$index] = $temp;
     }

  }
  return $numbers;

}

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


Donate

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

My Personal PayPal.

22条评论

  1. 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 zeal the same as my very own to understand whole lot more with respect to this problem. I think there are thousands of more pleasurable times up front for those who view your website.

  2. 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 advantageous guidance 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 brilliant job.

  3. My wife and i were very thrilled when Raymond managed to round up his web research with the precious recommendations he was given while using the weblog. It is now and again perplexing to simply possibly be releasing tips and tricks which usually the rest might have been trying to sell. We really understand we now have you to thank for this. Those illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly wonderful, and it is making our son in addition to us understand this topic is thrilling, which is certainly wonderfully vital. Thanks for the whole thing!

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

  5. I am commenting to let you know of the fantastic encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  6. I am commenting to let you know of the fine encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people quite simply grasp chosen hard to do subject areas. You really exceeded our expectations. Many thanks for giving the insightful, healthy, educational and as well as easy thoughts on the topic to Jane.

  7. I’m just commenting to let you be aware of of the fabulous discovery my cousin’s daughter experienced going through your site. She even learned lots of issues, most notably how it is like to have a very effective giving mindset to have many others just gain knowledge of chosen grueling matters. You really did more than our desires. Many thanks for distributing the important, healthy, educational and as well as easy thoughts on the topic to Gloria.

  8. 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 area of interest 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.

  9. My spouse and i have been really joyous that Jordan managed to do his research through your precious recommendations he had through the web pages. It is now and again perplexing to just find yourself handing out secrets and techniques which people today may have been selling. So we know we need the writer to give thanks to for that. The most important illustrations you have made, the simple web site navigation, the relationships you will help to instill – it’s got most powerful, and it’s really leading our son in addition to the family reason why the situation is fun, which is really mandatory. Thank you for the whole lot!

  10. I’m just commenting to let you be aware of of the extraordinary discovery my cousin’s daughter enjoyed going through your blog. She discovered lots of issues, most notably how it is like to have a very effective giving heart to have many others effortlessly gain knowledge of certain extremely tough matters. You really did more than my desires. Many thanks for displaying the helpful, healthy, educational and also easy thoughts on the topic to Gloria.

  11. Thanks for your own work on this website. My mum take interest in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very helpful tips and hints via your website and therefore strongly encourage response from visitors on this subject matter while our own simple princess is without a doubt studying so much. Take pleasure in the rest of the year. Your performing a tremendous job.

  12. My spouse and i got quite glad 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 methods that many other people 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 great, 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!

  13. My wife and i were very satisfied when Peter managed to round up his survey while using the precious recommendations he received when using the weblog. It is now and again perplexing to simply possibly be offering tips which usually the others might have been trying to sell. We really see we now have you to thank for this. These illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly terrific, and it is making our son in addition to us recognize that this theme is satisfying, which is certainly unbelievably vital. Thanks for the whole thing!

  14. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render worthwhile tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a wonderful job.

  15. My wife and i were very satisfied when Peter managed to finish up his survey while using the precious recommendations he received when using the weblog. It is now and again perplexing to simply possibly be offering tips which usually the others might have been trying to sell. We see we now have you to thank for this. These illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly terrific, and it is making our son in addition to us recognize that this theme is satisfying, which is certainly unbelievably serious. Thanks for the whole thing!

  16. I precisely needed to appreciate you once again. I am not sure the things that I might have gone through in the absence of the entire opinions documented by you directly on such a industry. It had been a very fearsome difficulty in my circumstances, however , finding out the skilled manner you managed that made me to jump over fulfillment. I am happier for this help and even hope you find out what a great job you are doing instructing other individuals through the use of your site. I know that you have never encountered all of us.

  17. Very nice post and right to the point. I don’t know if this is really the best place to ask but do you guys have any ideea where to hire some professional writers? Thanks in advance 🙂 https://livermedi.com liver pain drugs

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

  19. Very nice post and right to the point. I don’t know if this is really the best place to ask but do you guys have any ideea where to hire some professional writers? Thanks in advance 🙂 https://livermedi.com liver pain meds

  20. Awsome article and right to the point. I don’t know if this is truly the best place to ask but do you people have any ideea where to employ some professional writers? Thx 🙂 https://arthritismedi.com buy Arthritis meds

  21. I am always searching online for articles that can help me. Thank you! https://hyperactivitymed.com causes of hyperactivity in children

  22. I learned more new things on this weight loss issue. One issue is that good nutrition is highly vital when dieting. A huge reduction in fast foods, sugary foods, fried foods, sugary foods, red meat, and white flour products may be necessary. Holding wastes parasites, and toxins may prevent goals for losing fat. While certain drugs temporarily solve the problem, the nasty side effects are not worth it, and they never offer more than a temporary solution. It is a known fact that 95% of fad diets fail. Thanks for sharing your ideas on this blog. https://osteoporosismedi.com best prescription drugs for osteoporosis

发表评论

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