Python implementation code for sorting algorithm selection

Source: Internet
Author: User

1. algorithm:
For a set of keywords {K1, K2 ,..., Kn}, first from K1, K2 ,..., Select the minimum value in Kn. If it is Kz, Kz and K1 are switched;
Then, from K2, K3 ,... , Select the minimum value Kz in Kn, and then swap Kz and K2.
So select and change the N-2 trip, the (n-1) trip, from the Kn-1, Kn select the minimum Kz and Kn-1 swap, the last is the maximum value in the sequence, A small to large ordered sequence is formed in this way.

2. Select the sorting code for python:

Copy codeThe Code is as follows:
Def selection_sort (list2 ):
For I in range (0, len (list2 )):
Min = I
For j in range (I + 1, len (list2 )):
If list2 [j] <list2 [min]:
Min = j
List2 [I], list2 [min] = list2 [min], list2 [I] # swap

Result: [2, 3, 4, 21, 33, 44, 45, 67]

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.