Implementation code for Python selection sorting algorithm

Source: Internet
Author: User
1. Algorithm:
For a set of keywords {k1,k2,..., kn}, first select the minimum value from the K1,k2,..., Kn, and if it is a KZ, swap the KZ with the K1;
Then choose the minimum KZ from K2,k3, ..., kn, and then swap the KZ with the K2.
So choose and Exchange N-2, (n-1), select the minimum value from Kn-1, kn the KZ converts the KZ to Kn-1, and the last is the maximum value in the sequence, which is formed by an ordered sequence of small to large.

2.python Select Sort Code:

Copy the Code code 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

The results are: [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.