NO.2 选择排序





什么是排序

 
       所谓排序,就是让一串数据,能够按照我们需要的顺序(如:递增和递减)排列的操作。
 
       就比如说新学校大家都第一次见面,排座位的时候就会让大家排成排,比一比高矮。一般都是长得【可爱迷你】的同学站在前面,长得【帅气拔高】的就站在后面。
 
 
        额,当然提起这个话题,总会有人摸一把辛酸泪,还是少提为妙啊。
       
 
       当然啦,大家再看一看我们的标题,不是排序,而是【选择排序】。嘿嘿。
 
       这就代表这选择的方法其实有很多种啦,选择排序呢就是其中一种(大概算是最简单的一种?)排序方式。让我们先来看一看吧~
 

选择排序

       选择排序(Selection sort),是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。
       嗯……嗯?
 
       好的,已经可以想象有人表情可能会是酱的:
       于是,果然还是要我举个例子嘛!\(^o^)/
 
       仍然是排队。
       现在有五个来自巨人国的巨人,1-5号,身高分别为1-5米(中间忽现侏儒啊……)。现在他们中了混乱的技能,排队成这样:
       3-5-2-4-1
       现在我希望他们从矮到高排列,虽然还是不养眼,至少舒服多了。
        此时,我们应该:
        ①拎出最矮的那一个(也就能拎这个了),也就是1号,把他和第一个人换一下~变成这样:
       1-5-2-4-3
        ②再从剩下四个里面又找出最矮的那一个,也就是2号,拖出来和第二个换一下~变成这样:
       1-2-5-4-3
       
        ③再从剩下三个里面又找出最矮的那一个,也就是3号,用吊车吊起来和第三个换一下~变成这样:
       1-2-3-4-5
       
        ④于是这个时候就有人以为结束了……呵呵。
        拖出去打一百遍啊一百遍。
        你看见了,计算机可看不见。根据以上规律,我们应该再从剩下两个里面又找出最矮的那一个,也就是4号,用……(想不出来用什么了)和第四个换一下~变成这样:
       1-2-3-4-5
        嗯?什么?你说跟之前一样?觉得我
      
 
        好吧,欢迎你用这道题试试:
        背景同上,混乱后排序如下:
        3-5-4-2-1
        嘿嘿,你会发现只做三步会变成这样:
        1-2-3-5-4
        所以最后一步是很有必要的!
 
 
PS: 以上表情包若不符你的真实情况,请勿在意~
那么,最后帮大家总结一下优缺点。
 

优点

         非常直观,在代码编写时比较方便,简单易懂。
 

缺点

       耗时比较长。想象一下,有n个数据,就要查n-1次,每次挑出一个跟之前的换。上文举的例子只有5个数据,如果是学校所有同学,上百上千怎么办?凉拌!还是只能查这么多遍!而且要是遇上两个优先度相同的数据(比如两个5),电脑可能会内心草泥马奔腾,一脸黑人问号表情包,然而最后还是不知道该选哪个。
 
      所以……还是要建议大家,选择排序一般要用于比较简单的,优先度差距较大的数据的排序哦!
 
以上部分内容来自网络,图片要感谢度娘~
若有版权问题,请联系我们!谢谢!

《NO.2 选择排序》上有347条评论

  1. I’m just writing to make you know what a magnificent experience our girl encountered using yuor web blog. She noticed such a lot of pieces, including what it’s like to possess a marvelous giving nature to make most people very easily completely grasp a variety of tortuous subject matter. You really exceeded visitors’ desires. Many thanks for imparting these practical, safe, educational and also unique tips on your topic to Sandra.

  2. I wish to express my thanks to you for bailing me out of this type of issue. Because of browsing throughout the the web and meeting proposals that were not powerful, I thought my entire life was done. Existing without the presence of strategies to the issues you have sorted out by way of the site is a critical case, and those which may have adversely damaged my entire career if I hadn’t noticed your website. Your competence and kindness in controlling every part was excellent. I am not sure what I would have done if I hadn’t come upon such a solution like this. It’s possible to at this point look forward to my future. Thank you so much for the professional and results-oriented guide. I will not be reluctant to suggest your web page to anyone who will need assistance about this matter.

  3. I’m also commenting to let you understand what a superb discovery our girl gained studying the blog. She even learned a wide variety of things, most notably how it is like to possess an incredible teaching mindset to get the rest smoothly fully grasp various tortuous topics. You undoubtedly did more than our expectations. I appreciate you for producing those warm and friendly, safe, explanatory and also unique guidance on this topic to Jane.

  4. I intended to create you this little remark in order to give many thanks yet again for all the wonderful ideas you have provided above. This is really unbelievably generous with you to make publicly just what many of us would’ve offered as an e-book to get some profit for their own end, principally considering the fact that you might have done it if you desired. These guidelines likewise acted like a easy way to fully grasp the rest have the same dream really like my own to grasp a lot more when considering this issue. I know there are a lot more fun sessions in the future for many who looked over your website.

  5. I together with my guys ended up checking the excellent strategies found on the website then instantly came up with an awful feeling I had not expressed respect to the web blog owner for those techniques. All the people came as a consequence passionate to study all of them and have undoubtedly been having fun with those things. Thank you for really being considerably accommodating and then for making a decision on varieties of terrific useful guides millions of individuals are really desperate to be aware of. My sincere apologies for not expressing gratitude to sooner.

发表评论

电子邮件地址不会被公开。