Classic sorting algorithm-select sorting selection sort

Source: Internet
Author: User

Classic sorting algorithm-select sorting selection sort

The simplest way is to directly select a minimum (or maximum) number from the array to be sorted, and get a minimum number each time,

Place the data in the new array sequentially until all data is obtained.

Simply put, say to a group of arrays, who of you is the least out of the column and standing at the last side?

Then, we will continue to explain to the unordered array, who of you is the least out-of-the-column, standing on the last side

Continue the previous operation until the last one. Continue to the last side. Now the array is sorted, from small to large.

Example

First, let's look at the status changes in each step. Later we will introduce the details. The existing unordered array [6 2 4 1 5 9]

First, locate the minimum number 1 and put it on the frontend side (in exchange with the first digit)

Before exchange:| 6 | 2 | 4 | 1 | 5 | 9 |

After exchange:| 1 | 2 | 4 | 6 | 5 | 9 |

The second step is to find the minimum number 2 in the remaining number [2 4 6 5 9] and exchange it with the first number in the current array. Actually, there is no exchange, which is originally in the first place.

Before exchange:| 1 | 2 | 4 | 6 | 5 | 9 |

After exchange:| 1 | 2 | 4 | 6 | 5 | 9 |


The third step continues to find the minimum number 4 in the remaining number [4 6 5 9]. Actually, there is no swap.

The fourth step is to find the minimum number 5 from the remaining [6 5 9] and switch the position with the first number 6.

Before exchange:| 1 | 2 | 4 | 6 | 5 | 9 |

After exchange:| 1 | 2 | 4 | 5 | 6 | 9 |

The fifth step is to find the minimum number of 6 from the remaining [6 9], and find that it is waiting in the correct position without switching.

Output correct results after sorting [1 2 4 5 6 9]

First, find the details of the minimum number 1.

The current array is| 6 | 2 | 4 | 1 | 5 | 9 |

First, extract 6 and let it act as the minimum number.

The current minimum number 6 is compared with other numbers one by one. If the number is smaller, the role is switched.

Compare the current minimum number 6 and 2, find more decimal places, switch the role, then the minimum number is 2, then 2 compare with the remaining number

The current minimum number 2 and 4 do not move

Compare the current minimum number 2 and 1, find more decimal places, switch role, then the minimum number is 1, then 1 compare with the remaining number

Comparison between the current minimum number 1 and 5, not moving

The current minimum number 1 and 9 are compared, do not move, and reach the end

The current minimum number 1 is used to exchange positions with the current first number, as shown below:

Before switching: | 6 | 2 | 4 | 1 | 5 | 9 |

After switching: | 1 | 2 | 4 | 6 | 5 | 9 |

Complete sorting. The other steps are similar.

 

 

Back to main directory [classic Sorting Algorithm] [Collection]

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.