Python algorithm-Select sort

Source: Internet
Author: User

Select Sort:

First, the language description: From a list of numbers in turn select the smallest place, the second bit small, and so on to complete the sorting

    1. First traversal, find the smallest number, put it in the first position
    2. Second traversal, find the second largest number, put it in the second position
    3. ...... and so on, complete the sorting

Fixed position, find element

    1. for An array of n elements, start at the first position, find the smallest, and place it in the first position
    2. From the second position, the rest of the array, find the smallest, put in the second position
    3. ....
    4. Starting from the n-1 position, in the remaining array, find the smallest and place it in the n-1 position

Code implementation:

# Encoding=utf-8

def selectsort (LISTX):

Xlen = Len (LISTX)

For I in Xrange (Xlen):

Min = i

For j in Xrange (I+1,xlen):

If listx[min] > Listx[j]:

Min = J

Listx[i],listx[min] = Listx[min],listx[i]

Return LISTX

if __name__ = = ' __main__ ':

Print Selectsort ([32,34,55,66,5])

Exercise one: Change to descending order

#enconding = Utf-8

def select_sort (lista):

For i in Xrange (Len (lista)):

Min = i

For j in Xrange (I+1,len (lista)):

if (Lista[min] < lista[j]):

Min = J

Lista[min],lista[i] = Lista[i],lista[min]

return lista

if __name__ = = ' __main__ ':

Print Select_sort ([32,14,67,8,21])

Exercise 2: Does the code have an unnecessary comparison, can you subtract it, say why

# Encoding=utf-8

def selectsort (LISTX):

Xlen = Len (LISTX)

For I in Xrange (xLen-1):

Min = i

For j in Xrange (I+1,xlen):

If listx[min] > Listx[j]:

Min = J

Listx[i],listx[min] = Listx[min],listx[i]

Return LISTX

if __name__ = = ' __main__ ':

Print Selectsort ([32,34,55,66,5])

Second, the calculation of time complexity

32,34,55,66,5

First round:5,32,34,55,66 n-1 times

Second round:5,32,34,55,66 n-2 times

...

Fifth round:5,32,34,55,66 1 times

1+2+3+ +n-2+ n-1 = N (n-1)/2

The complexity of time is O (n^2)

Python 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.