def MinimumSwaps(Queue):
MinSwaps = 0
for i in range(len(Queue) - 1):
if Queue[i] != i+1:
for j in range(i+1,len(Queue)):
if Queue[j] == i+1:
Queue[i], Queue[j] = Queue[j], Queue[i]
MinSwaps += 1
break
else:
continue
return MinSwaps
def main():
Result = MinimumSwaps([7, 1, 3, 2, 4, 5, 6])
print(Result)
if __name__ == "__main__":
main()
The question: You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order.
The issue is that what I have provided is inefficient and fails on very large arrays, however Ive tried to optimise it as much as I can and im not aware of another technique to use. This question is likely related to a particular sorting algorithm but is there any way to modify the above code to make it faster?