# 分析

1. 序列没有任何元素，显然
2. 序列只有一个元素，显然，即一个元素的全排列只有一种，就是它本身。

# 算法

• 输入：一个有限的递增有序序列
• 输出：序列递增全排列集合
1. 初始化结果集
2. 若序列中仅有一个元素，或为空（此时），则，算法结束，返回
3. 否则，分别令，对于，递归地使用该算法计算剩余的有序序列的递增全排列集合，令，由此我们得到了以为固定首位的全排列集合。从而归并到结果集合中
4. 由于中元素是有限的，所以算法的递归过程一定会到达2中的状态并终止，从而算法在有限步骤后一定会终止。

# 代码

function generatePermutation(n){
let seq=(length=>Array.from({length},(\$,i)=>i+1))(n),result=[];
//Require substring of seq starting from start is a rigidly-ascending ordered sequence,
//namely, for any i,j with start<=i<j<length, seq[i]<seq[j] holds.
(function permutation(seq,result,start=0){
//recursive function for all permutations of seq with a specified start index(counting from 0).
//The permutation of single element is this element itself, so stop here.
start===seq.length-1 && result.push(Array.from(seq));

//Now,we can get all permutations by the following:
//  Permutation(<a1,a2,...,an>)
//  =SUM_[from i=1 to n] ( ai + Permutation(<a1,a2,...,a_{i-1},a_{i+1},...,an>) )
//  where a1<a2<...<an, and notation <...> stands for an ordered sequence.
for(let i=start;i<n;i++){
// to achieve this, we can move ai to the head, leaving the remnants,say,
// <a1,a2,...,a_{i-1},a_{i+1},...,an> ,still ordered. Thus, we apply permutation
// on these remnants, which turns to be a subproblem.
// NOTICE: if ascending ordered permutations are required, we use function move.
// Otherwise, a simple swap can be used here.
//
move(seq,i,start);      //swap(seq,i,start);
permutation(seq,result,start+1);
move(seq,start,i+1);    //swap(seq,i,start);
}
})(seq,result);
return result;
}

//move seq[from] to before seq[to]
function move(seq,from,to){
[[to,0,seq[from]],[from+(from>to),1]].forEach(arr=>seq.splice(...arr))
}

// swap seq[i1] with seq[i2]
function swap(seq,i1,i2){
[seq[i1],seq[i2]]=[seq[i2],seq[i1]];
}

generatePermutation(4)