"Python Learning notes-data structures and algorithms" Selection sorting Selection sort

Source: Internet
Author: User

"Select Sort"

The selection sort is based on a bubbling sort (Bubble sort) that has been improved: each visit process (pass) needs to be exchanged at most.

Each visit process, to find the maximum value, when the end of the visit, the maximum value is exchanged to the correct position;

Then continue to repeat the process in the remaining sublist until the n-1 visit is completed (n is the length of the list);

At this point, the remaining elements in the list are automatically aligned to the correct position.

A list of length n=9 is given: The first visit finds the maximum value of 93 and swaps it to the far right of the list, that is, Exchange 93 and 20, and the second visit finds the maximum value in the remaining list 77 and says it swaps to the far right of the remaining list, that is, 77 and 55, and so on until the n-1=8 visit is completed, The list sort is complete. For the fifth time in the example, 44 is the maximum value in the remaining list, which happens to be in the correct position, and does not need to be exchanged.

    

  

"Implementation of selection sort"

During the visit we can select the minimum value to swap to the leftmost part of the list, or you can select the maximum value to swap to the far right of the list. Here, for example:

  

"Performance Analysis"

The number of comparisons is the same as the Bubble sort O (N2), but the number of exchanges decreases compared to the bubble sort.

"Python Learning notes-data structures and algorithms" Selection sorting Selection sort

Related Article

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.