Vernacular sorting algorithm--select sort

Source: Internet
Author: User

Objective:

  Continue the sorting method, immediately after the morning of the insertion sort , immediately following, and now with the new on the Select sorting algorithm

Select Sort: First find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue to find the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence . And so on until all elements are sorted.

Scenario Description:

New term for the new sports teacher, facing the same queue situation, he said: "I do not care how to teach before, after all according to my to do, we choose the sort ."

    1. Starting from the first classmate, and the second classmate compared, who is short, who will remember the current oneself is the shortest (can stand in front, hey), and then the third classmate compared ... The shortest in turn and behind each classmate comparison (here the shortest is real-time change), and finally ran to the first position of the sorting queue for position swap
    2. To exclude the classmates who have already lined up, repeat Step 1, select one of the shortest, stand to the first of the queue, until the last person is left, arrange to complete

  Some students say : "This is not the same as the old wet said the bubble of it."

  Some students said : "Not the same, feel less laborious."
old Wet said : "All right, thought and bubbling is the same, every time to choose to arrange the inside-the highest or lowest corresponding station to the last or the first, but, bubbling it every time the comparison is to change position, and I this choice does not need to change every time, we first put the current highest or lowest remember ( cache up ) , take the cache and the back of the person, there is no need to each adjacent to get 22 comparison ... "

Students instantly dawu, big wet clever ~!

, the yellow part is the first cached object (changing at any time) for each new round of sorting, and the darker part is the sorted list

Code snippet:

/*** Select Sort, starting from the current bit (0 start) and the next one to compare, find the smallest element, let the current bit and the smallest element transposition, the other element is unchanged * Continue to start from the next one in the current bit, find the smallest element and swap until the last one *@paramarr *@return     */     Public Static int[] Selectsort (int[] arr) {        intLen =arr.length;  for(inti = 0; i < Len; i++) {            intmin = Arr[i];//The first bit of the current queue is assumed to be the minimum value and is cached every time            intindex = i;//cache Minimum (change) coordinates             for(intK = i + 1; K < Len; k++) {//Backward Comparison                if(Arr[k] <= min) {//If the latter one is less than or equal to the previousmin = arr[k];//Change the minimum valueindex = k;//change the minimum value coordinates                }            }            if(min = = Arr[i])//equality does not change, the default is to retain the first of the current queue for the smallest                Continue; //after the loop is completed, the coordinates of the minimum value are found, that is index, the current coordinate I element and the minimum element are swapped            intK =Arr[i]; Arr[i]=Arr[index]; Arr[index]=K; }        returnarr; }

Pros: fewer mobile data

Cons: multiple comparisons, and no stability due to element arrangement

                              Writing is not easy, inevitably there are omissions and mistakes, please also be generous, please recommend

  PS: Welcome reprint, reproduced please indicate the source: http://www.cnblogs.com/liuyitian/p/4054437.html

                                          Learn a little bit more everyday code less knock a little bit

Vernacular sorting algorithm--select sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.