While True:
if playersLeft()==False:
break
thisClan=getClanWithTheMostFreeParticipants()
if randint()%2 == 1:
if checkIfClanWithBiggerRatingHasFreeParticipant(thisClan) ==True:
pair(thisClan, clanWithClosestBiggerRating(thisClan) #pairs 2 random participants from these clans
else:
if checkIfClanWithLowerRatingHasFreeParticipant(thisClan) ==True:
pair(thisClan, clanWithClosestLowerRating(thisClan) #pairs 2 random participants from these clans
else:
giveFreeWin(thisClan)
break
else:
if checkIfClanWithLowerRatingHasFreeParticipant(thisClan) ==True:
pair(thisClan, clanWithClosestLowerRating(thisClan) #pairs 2 random participants from these clans
else:
if checkIfClanWithBiggerRatingHasFreeParticipant(thisClan) ==True:
pair(thisClan, clanWithClosestBiggerRating(thisClan) #pairs 2 random participants from these clans
else:
giveFreeWin(thisClan)
break

Time complexity: O(n^2)

Edited 9/15/2021 17:24:06