Select sort and Python implementations

Source: Internet
Author: User

1. Algorithmic thinking

n elements, assuming that the first I element is already ordered, the smallest element from i+1 to n elements is placed in its final position in the ordered table

2. Code implementation

(1) Non-recursive calls

defSelectionsort (A, N): forIinchRange (0, n-2): Mini=I forJinchRange (i+1, N):ifA[J] <A[mini]: I=J A[i],a[mini]= A[mini], a[i]#Swap        PrintAif __name__=='__main__': A= [6,5,4,3,2,1] Selectionsort (A, Len (a))PrintA

(2) Recursive invocation

defSelectionsort (A, K, n):if(k = = N):returnMini=k forJinchRange (k, n):ifA[J] <A[mini]: Mini=J A[k],a[mini]= A[mini], a[k]#Swap            Printa Selectionsort (A, K+1, N)if __name__=='__main__': A= [6,5,4,3,2,1] Selectionsort (A, 0, Len (a))PrintA

3. Complexity of Time

For any input, the selection sort is an O (n^2) algorithm.

Select sort and Python implementations

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.