[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}

2021-06-20 23:05

阅读:572

标签:numbers   ati   you   one   def   ant   ber   algorithm   none   

def permutationN(n):
	a=[None]*n
	for i in range(n):
		a[i]=i+1

	sum=1

	for j in range(n):
		sum*=(j+1)

	i=0
	for k in range(sum):
		# If you want to use stack
		#a[i:i+2]=swapStack(a[i],a[i+1])
		a[i:i+2]=[a[i+1],a[i]]
		print(a)
		i+=1
		if i==(n-1):
			i=0

  

[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}

标签:numbers   ati   you   one   def   ant   ber   algorithm   none   

原文地址:https://www.cnblogs.com/chiyeung/p/9686007.html


评论


亲,登录后才可以留言!