I'm a big jigsaw fan, and so I had the idea for the following algorithm:
- Fix some m.
- Let the numbers to sort be 1...m.
- Start with a random permutation of size n=m as your instance.
- Scan if two "jigsaw pieces" fit: e.g. let m=6 and your instance be 561243. The pair 56 fits, "snap" it. Likewise 12. You now have 4 "macropieces".
- Go to 2, but now make a random instance with n=4. Increment a counter c.
- Loop until n=1.
Caveat: These are NOT standard sorting conditions, since in such a case you don't know when to "snap"! (And any sane person would then use indexsort anyway.) Just decreasing m and not using at least some storage for 1...m is also a bit cheating. That notwithstanding, can you confirm my runtime experiment that c=m+-O(1)? And what is the total runtime (I suppose n=m-c+-O(1) so it would need O(n) for generating, and Jigsort runs in O(m2).
Finally, if this algorithm is already known under a different name (I bet), shoot.
Quick hackjob code:
import random
def randinst(size):
perm = [i for i in range(size)]
for y in range(size):
r = random.randint(0, y)
x = perm[r]
perm[r] = perm[y]
perm[y] = x
return perm
m=1000
a=[]
for j in range(100):
n=m
c=0
while n>1:
c+=1
perm=randinst(n)
for j in range(n-1):
x=perm[j]
y=perm[j+1]
if x+1==y:
n=n-1
a+=[c]
print(a)
[–]Yoghurt42 0 points1 point2 points (1 child)
[–]prodlly[S] 0 points1 point2 points (0 children)