Skip to content

Instantly share code, notes, and snippets.

@SqrtRyan
Created November 15, 2025 16:25
Show Gist options
  • Select an option

  • Save SqrtRyan/a6a7f0e821cc1aaa1af6d47ed2cbe662 to your computer and use it in GitHub Desktop.

Select an option

Save SqrtRyan/a6a7f0e821cc1aaa1af6d47ed2cbe662 to your computer and use it in GitHub Desktop.
#CYCLESORT!!!
def swap(a,i,j):
a[i],a[j]=a[j],a[i]
class Counted:
def __repr__(self):
return 'X'
X=Counted()
a=shuffled([3,1,7,8,8,3,3,3,2,4,5,5,0])
n=len(a)
assert max(a)<n
z=[]
for i in range(n):
while i!=a[i] and a[i]!=X:
j=a[i]
k=a[j]
if j==k:
#Swap would make no difference
z+=[j]
a[i]=X
else:
swap(a,i,j)
print(a)
#CYCLESORT!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment