算法系列-选择排序

phpman.song@gmail.com发布

定义

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

原理

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

分类: 算法

16 条评论

cbd online · 2021年1月7日 上午7:44

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.

viagra without a prescription · 2021年1月8日 上午12:58

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.

CITALOPRAM tablets · 2021年1月8日 下午3:26

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!

buy geodon · 2021年1月8日 下午9:09

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!

order lexapro · 2021年1月8日 下午10:04

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.

发表评论

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