It is an interesting exercise to write a program to print out all permutations of $1, 2, …, n$. However, since there are $6227020800$ permutations of $1, 2, …, 13$, it is unlikely that we would ever run this program on an input of size more than $10$.
However, here is another interesting problem whose solution can also be used to generate permutations. We can order the permutations of $1, 2, …, n$ under the lexicographic (or dictionary) order. Here are the permutations of $1,2,3$ in lexicographic order:
123132213231312321123132213231312321 1 \, 2 \, 3 \quad    1 \, 3 \, 2 \quad     2 \, 1 \, 3 \quad     2 \, 3 \, 1 \quad     3 \, 1 \, 2 \quad     3 \, 2 \, 1 
The problem we have is the following: given a permutation of $1,2, …, n$, generate the next permutation in lexicographic order. For example, for $2 3 1 4$ the answer is $2 3 4 1$.

-----Input:-----
The first line of the input contains two integers, $N$ and $K$. This is followed by $K$ lines, each of which contains one permutation of $1, 2,…,N$.

-----Output:-----
The output should consist of $K$ lines. Line $i$ should contain the lexicographically next permutation correponding to the permutation on line $i+1$ in the input.

-----Constraints:-----
- $1 \leq N \leq 1000$.
- $1 \leq K \leq 10$.

-----Sample input-----
3 2
3 1 2
2 3 1

-----Sample output-----
3 2 1
3 1 2

Difficulty: interview

Starter code:

Solutions:
import sys
# import math as mt
# from collections import Counter
# from itertools import permutations
# from functools import reduce
# from heapq import nsmallest, nlargest, heapify, heappop, heappush, heapreplace

def get_inpt(): return sys.stdin.readline().strip()
def get_int(): return int(sys.stdin.readline().strip())
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

# sys.setrecursionlimit(10**7)
# INF = float('inf')
# MOD1, MOD2 = 10**9+7, 998244353

n, k = get_ints()

for _ in range(k):
    
    arr = get_array()
    
    for i in reversed(range(n-1)):
        
        if arr[i] < arr[i+1]:
            
            ind = i+1
            minn = arr[i+1]
            for j in range(i+1, n):
                if arr[j] > arr[i]:
                    minn = min(arr[j], minn)
                    ind = j
                    
            arr[i], arr[ind] = arr[ind], arr[i]
            
            arr = arr[:i+1] + sorted(arr[i+1:])
            
            break
    
    print(*arr)

class Solution(object):
   def nextPermutation(self, nums):
      found = False
      i = len(nums)-2
      while i >=0:
         if nums[i] < nums[i+1]:
            found =True
            break
         i-=1
      if not found:
         nums.sort()
      else:
         m = self.findMaxIndex(i+1,nums,nums[i])
         nums[i],nums[m] = nums[m],nums[i]
         nums[i+1:] = nums[i+1:][::-1]
      return nums
   def findMaxIndex(self,index,a,curr):
      ans = -1
      index = 0
      for i in range(index,len(a)):
         if a[i]>curr:
            if ans == -1:
               ans = curr
               index = i
            else:
               ans = min(ans,a[i])
               index = i
      return index
ob1 = Solution()
n,k=map(int,input().split())
l=[]
for i in range(k):
    t=list(map(int,input().split()))
    l.append(t)
for i in l:
    print(*ob1.nextPermutation(i))


n, t = list(map(int, input().split()))
while(t):
    t -= 1
    a = list(map(int, input().split()))
    i = n-1
    while(i > 0 and a[i] < a[i-1]):
        i -= 1
    j = i
    i -= 1
    mini = j
    while(j < n):
        if(a[j] > a[i] and a[j] < a[mini]):
            mini = j
        j += 1
    a[i], a[mini] = a[mini], a[i]
    a[i+1:] = sorted(a[i+1:])
    print(*a)

n, k=map(int, input().split())
for i in range(k):
    arr=list(map(int, input().split()))
    res=[]
    
    for i in range(len(arr)-1, 0, -1):
        if(arr[i]>arr[i-1]):
            res.append(arr[i-1])
            res.append(arr[i])
            temp=arr[i-1]
            break
        else:
            res.append(arr[i])
        
    res.sort()
    for j in range(len(res)):
        if(res[j]>temp):
            arr[i-1]=res[j]
            res.remove(res[j])
            break
    
    for j in range(len(res)):
        arr[i]=res[j]
        i+=1
        
    print(*arr)