#!/usr/local/bin/python3 # Author: Dr. Robert Heckendorn, Computer Science Department, University of Idaho, 2013 from lib import * def reverse(x, a, b) : while b>a : (x[a], x[b]) = (x[b], x[a]) a+=1 b-=1 def perm(size) : x = list(range(0, size)) while True : print(x) # print permutation done = True for i in range(size-2, -1, -1) : if x[i]x[i] : break # finds the largest j such that x[j]>x[i] # we know that x[j]>x[i]>x[j-1] that is this is the place to insert x[i] # in the ordered suffix. # So insert it there (x[i], x[j]) = (x[j], x[i]) # produce the next lexigraphically minimum list reverse(x, i+1, size-1) perm(6)