-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum_number_of_swaps.py
More file actions
63 lines (49 loc) · 1.64 KB
/
minimum_number_of_swaps.py
File metadata and controls
63 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Python3 program to find minimum number
# of swaps required to sort an array
Numero di swap = somma sui cicli di (taglia del ciclo -1)
taglia del ciclo = numero di nodi nel ciclo
Soluzione trovata qui:
https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/
# Function returns the minimum
# number of swaps required to sort the array
def minSwaps(arr):
n = len(arr)
##e.g arr = [1, 5, 4, 3, 2]
arrpos = [*enumerate(arr)]
## arrpos=[(0, 1), (1, 5), (2, 4), (3, 3), (4, 2)]
# Sort arrpos so that:
# (position in arr, element)
arrpos.sort(key = lambda it:it[1])
# [(0, 1), (4, 2), (3, 3), (2, 4), (1, 5)]
# To keep track of visited elements.
# Initialize all elements as not
# visited or false.
vis = {k:False for k in range(n)}
# Initialize result
ans = 0 #ans stands for answer
for i in range(n):
# already swapped or
# already present at
# correct position
if vis[i] or arrpos[i][0] == i:
continue
# find number of nodes
# in this cycle and
# add it to ans
cycle_size = 0
j = i
while not vis[j]:
# mark node as visited
vis[j] = True
# move to next node
j = arrpos[j][0]
cycle_size += 1
# update answer by adding
# current cycle
if cycle_size > 0:
ans += (cycle_size - 1)
# return answer
return ans
# Driver Code
arr = [1, 5, 4, 3, 2]
print(minSwaps(arr))