Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Given array
def isSorted ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT def sortPoss ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT idx = - 1 NEW_LINE minVar = arr [ i ] NEW_LINE for j in range ( i , len ( arr ) , 2 ) : NEW_LINE if arr [ j ] < minVar : NEW_LINE INDENT minVar = arr [ j ] NEW_LINE idx = j NEW_LINE DEDENT if idx != - 1 : NEW_LINE arr [ i ] , arr [ idx ] = arr [ idx ] , arr [ i ] NEW_LINE DEDENT if isSorted ( arr ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT arr = [ 3 , 5 , 1 , 2 , 6 ] NEW_LINE print ( sortPoss ( arr ) ) NEW_LINE