Select the Sort python

Source: Internet
Author: User

Select sort (Selection sort) 1. Algorithm Description:
    • 2 linear passes on a list
    • At each pass, it chooses the smallest value
    • Swap it with the last unclassified element

2. Algorithm properties:
    • Algorithm time complexity: O (n**2)
    • Instability: Duplicate elements in list may change Order after selection
    • O (1) Extra space
    • O (n2) comparison
    • O (N) Interchange
    • Not adaptable: Do not add flag to improve like bubbling

3. Code implementation
#Kumata ' s code#algorithmic complexity O (n**2)#find the smallest element and exchange it with the first index#从小到大排

ImportTime
defSelection_sort (nums=list): Start=time.time ()#The first layer selects the nth small element subscript forIinchRange (len (nums)):#NPos_min = i#Index #second-level traversal to find the element subscript that needs to be swapped forJinchRange (i + 1, Len (nums)):ifNums[pos_min] >Nums[j]: pos_min=J#Exchange heheNums[i],nums[pos_min] =Nums[pos_min],nums[i] t= Time.time ()-Startreturnnums,tnums= [1,2,5,8,4,3,6]selection_sort (nums)#Output Results([1, 2, 3, 4, 5, 6, 8], 0.0)

Select the Sort python

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.