The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.
- Find the largest index
k
such that
a
[k
] <
a
[k
+ 1]
. If no such index exists, the permutation is the last permutation.
- Find the largest index
l
such that
a
[k
] <
a
[l
]
. Since
k
+ 1
is such an index,
l
is well defined and satisfies
k
<
l
.
- Swap
a
[k
] with
a
[l
].
- Reverse the sequence from
a
[k
+ 1
] up to and including the final element
a
[n
].
After step 1, one knows that all of the elements strictly after position
k
form a weakly decreasing sequence, so no permutation of these elements will make it advance in lexicographic order; to advance one must increase
a
[k
]. Step 2 finds the smallest value
a
[l
] to replace
a
[k
] by, and swapping them in step 3 leaves the sequence after position
k
in weakly decreasing order. Reversing this sequence in step 4 then produces its lexicographically minimal permutation, and the lexicographic successor of the initial state for the whole sequence.
package alg_test;
public class Permutation {
int count = 0;
void swap(int a[], int i, int j)
{
int temp = a[i];
a[i]=a[j];
a[j]= temp;
}
void printArray(int a[]){
for(int i=0;i<a.length;i++) System.out.print("\t"+a[i]);
System.out.println("");
}
boolean calcNextPermutation(int a[]){
int length = a.length;
if (length == 0 || length == 1) return false;
int i = length - 1;
while(a[i-1] > a[i]) {
i--;
if(i==0) return false;
}
int k = i-1;
int j = a.length - 1;
while(a[j] < a[k] ) j--;
int l = j;
swap(a, k, l);
k++;
j = a.length -1;
while(k<j){
swap(a, k, j);
k++;
j--;
}
count++;
printArray(a);
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Permutation p = new Permutation();
int a[] = {1,3,5,7,9};
p.count = 1;
p.printArray(a);
while(p.calcNextPermutation(a)){
}
System.out.println("Total " + p.count + " .");
}
}
分享到:
相关推荐
Considering the complex of the data required by Ministry of Transport of P.R.C every period of ten days, I design the following algorithm to generate the data required.
介绍如何在VanetMobiSim中产生NS2能够使用的trace file
How to use SFTP (with client validation - public key authentication) The topic How to use SFTP (with client validation - password authentication) discusses the simplest form of client ...
You will learn about the basic plots, how to customize them, and combine them to make sophisticated figures. Along with basic plots, you will also learn to make professional scientific plots.
Failed to Generate Report(解决方案).md
Failed to generate secure key pair(解决方案).md
Immersive Teleconferencing A New Algorithm to Generate Seamless Panoramic.pdf
- **5.2 How to tell Linux what code to generate when a key is pressed**:解释了如何告知Linux当按下某个键时应生成什么代码。 - **5.3 How to tell Xt to interchange Delete and Backspace**:讲解了如何让...
Excel Macro to Generate Database Insert Script Excel宏生成数据库插入脚本 对于开发人员来说,一次在数据库中添加/编辑主数据或静态数据一直很麻烦,因为它需要为每个小的文本更改集编写脚本。 对于不了解SQL...
例如,给出的`memory_to_vector`模块就是一个利用generate语句将二维数组转换为一维向量的例子。 总之,`generate`是Verilog编程中一个强大的工具,它提供了在硬件描述语言中创建复杂并行结构和条件实例化的能力,...
How to generate a runnable jar QArt4j is a maven project, run the following command to get a ruunable jar: mvn compile assembly:single This will generate a runnable jar under target/ directory. How to...
《CheckSum_Generate_exe_v7.1536.00.00:MTK平台校验和生成工具详解》 在IT行业中,数据的完整性和安全性至关重要,尤其是在固件更新、软件分发等场景下。为了确保文件在传输过程中没有被篡改或损坏,通常会使用...
you can add & view records. It is automatically generate on EXCEL, MSWORD, NOTEPAD and also to any other office encoder. How to change extension format files LIKE .csv .doc .txt
You will discover how to build an engaging Unreal iOS game, how to generate revenue, and how to optimize game performance using the tools and functionalities the Engine provides. To begin, you will ...
《codegenerate-3.6.1源码解析与二次开发指南》 在IT行业中,源码分析和二次开发是提升软件功能、优化性能的重要手段。本文将深入探讨"codegenerate-3.6.1源码",它是基于Jeecg框架的自动生成代码工具的源代码版本...
在使用QUARTUS II进行FPGA项目开发时,编译过程中可能会遇到“Error: Run Generate Functional Simulation Netlist”的错误提示,这通常是由于缺少仿真网表导致的。在解决这个问题之前,我们首先要理解QUARTUS II的...