Python- selection sorting algorithm

# Selection sort, time complexity O (N²) 
DEF select_sort (ARR):
     "" " 
    First, find the smallest sequence of unsorted (large) element, the starting position is stored in the sorted sequence. 
    Was continued from the remaining elements unsorted find the minimum (large) element, sorted and then put the end of the sequence. 
    repeat step until all the elements are sorted. 
    : param arr: 
    : return: 
    "" " 
    for i in the Range (len (arr) - 1 ) :
         # record index minimum number, set i is the minimum number of index 
        MIN_VAL = i
         for J in Range (i +. 1 , len (ARR)):
             IF ARR [J] < ARR [MIN_VAL]:
                 # find a smaller number of replacement 
                MIN_VAL = J
         # i is not the smallest number, and i is the minimum number of exchange
        if the! = min_val: 
            arr [i], arr [min_val] = arr [min_val], arr [i]

 

Guess you like

Origin www.cnblogs.com/zivli/p/11122481.html