A sorting algorithm that slightly improves on selection sort | Python3 program to implement min max selection sort . ; shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code
def minMaxSelectionSort ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT min = arr [ i ] NEW_LINE max = arr [ i ] NEW_LINE min_i = i NEW_LINE max_i = i NEW_LINE for k in range ( i , j + 1 , 1 ) : NEW_LINE INDENT if ( arr [ k ] > max ) : NEW_LINE INDENT max = arr [ k ] NEW_LINE max_i = k NEW_LINE DEDENT elif ( arr [ k ] < min ) : NEW_LINE INDENT min = arr [ k ] NEW_LINE min_i = k NEW_LINE DEDENT DEDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ min_i ] NEW_LINE arr [ min_i ] = temp NEW_LINE if ( arr [ min_i ] == max ) : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ min_i ] NEW_LINE arr [ min_i ] = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ max_i ] NEW_LINE arr [ max_i ] = temp NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT print ( " Sorted ▁ array : " , end = " ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 23 , 78 , 45 , 8 , 32 , 56 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE minMaxSelectionSort ( arr , n ) NEW_LINE DEDENT