the art of
Algorithm
Notes on Analysis and Design



Permutation

Following Program is to generate all permutations of a given list without using inbuilt module in python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def permutations(lis):
	#if list is empty then simply return
	n=len(lis)
	if n==0:
		return 0
	if n==1:
		return [lis]
	#Create an empty list that will store current permutations
	l=[]
	for i in range(n):
		m=lis[i]
		#extract m from the list and
		#create a new remaning list
		remlist=lis[:i]+lis[i+1:]
		for p in permutations(remlist):
			l.append([m]+p)
	return l
#Main Program
data = list('123')
for p in permutations(data):
    print p