I am trying to sort list in decreasing order and get the number of swaps required to sort the list in decreasing order [3, 1, 2] → [3, 2, 1]. i.e from the highest element of the list to the lowest using python. The function i have is sorting the list in increasing order i.e [3, 1, 2] → [1, 2, 3] . How would i sort it in decreasing order and get the number of swaps it took to sort the list?
def count_inversions(ratings):
swap = 0;
for i in range(len(ratings)):
if(i + 1 != ratings[i]):
t = i
while(ratings[t] != i+1):
t++
temp = ratings[t]
ratings[t] = ratings[i]
ratings[i] = temp
swap = swap + 1
return swap