We present a new algorithm for iterating over all permutations of a sequence. The algorithm leverages elementary operations on recursive lists. Within each recursive call, only two operations are required to generate all permutations (albeit in an unusual order): swapping the first two elements of the list or moving the last element to the front. As a result, no new nodes are allocated during the computation. Instead, all elements are rearranged within the original nodes of the singly linked list throughout the process. A proof of concept written in the Lisp programming language is proposed and discussed.
翻译:本文提出了一种遍历序列所有排列的新算法。该算法利用递归链表的基本操作。在每个递归调用中,仅需两种操作即可生成所有排列(尽管顺序非常规):交换链表前两个元素或将末尾元素移至前端。因此,计算过程中无需分配新节点,所有元素均在单向链表原始节点内完成重排。文中提出并讨论了用Lisp编程语言编写的概念验证实现。