Skip to content

Commit

Permalink
Added Heap's Algorithm
Browse files Browse the repository at this point in the history
Generates permutations of given numbers
  • Loading branch information
sulphatet authored Mar 2, 2022
1 parent b7f02ee commit e2a9782
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions generate_permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def generate(A,k):
if k ==1:
print(A)
return
else:
for i in range(k):
generate(A,k-1)
if(i<k-1):
if k%2 == 0:
A[i],A[k-1] = A[k-1],A[i]
else:
A[0],A[k-1] = A[k-1],A[0]

A = [1,2,3,4] #test-case
x = len(A)
generate(A,x)

0 comments on commit e2a9782

Please sign in to comment.