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