input = [10, 9, 7, 18, 13, 19, 4, 20, 21, 14]
Output : 10 9 18 7 20 19 4 13 14 21
def fixing(arr):
oddIdx = 1
evenIdx = 0
while True:
while evenIdx < len(arr) and arr[evenIdx] %2 ==0:
evenIdx +=2
while oddIdx < len(arr) and arr[oddIdx] % 2!=0:
oddIdx+=2
if evenIdx < len(arr) and oddIdx < len(arr):
arr[evenIdx], arr[oddIdx] = arr[oddIdx], arr[evenIdx]
else:
break
return arr
Is it because we are not ignoring the outer loop? If yes, why and if not, why not? Thank you!