Sort Array such that smallest is at 0 th index and next smallest it at last index and so on | Python3 program for the above approach Function to perform the rearrangement ; Initialize variables ; Loop until i crosses j ; This check is to find the minimum values in the ascending order ; Condition to alternatively iterate variable i and j ; Perform swap operation ; Increment i ; Assign the value of min ; Perform swap ; Decrement i ; Assign the value of min ; Print the array ; Driver Code ; Given Array arr [ ] ; Function call
def rearrange ( a , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE min = 0 NEW_LINE x = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT for k in range ( i , j + 1 ) : NEW_LINE INDENT if ( a [ k ] < a [ min ] ) : NEW_LINE INDENT min = k NEW_LINE DEDENT DEDENT if ( x % 2 == 0 ) : NEW_LINE INDENT temp = a [ i ] NEW_LINE a [ i ] = a [ min ] NEW_LINE a [ min ] = temp NEW_LINE i += 1 NEW_LINE min = i NEW_LINE DEDENT else : NEW_LINE INDENT temp = a [ j ] NEW_LINE a [ j ] = a [ min ] NEW_LINE a [ min ] = temp NEW_LINE j -= 1 NEW_LINE min = j NEW_LINE DEDENT x += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE rearrange ( arr , N ) NEW_LINE DEDENT