# algorithm来查找给定string的下一个更大的排列

1. find`s[i] < s[i+1]`的最高指数。 如果不存在这样的指数，排列是最后的排列。
2. find最高指标`j > i`这样`s[j] > s[i]` 。 这个`j`必须存在，因为`i+1`就是这样一个索引。
3. `s[j]`交换`s[i]` `s[j]`
4. 颠倒索引`i`之后的所有元素的顺序直到最后一个元素。

` `function nextPermutation(array) { var i = array.length - 1; while (i > 0 && array[i - 1] >= array[i]) { i--; } if (i <= 0) { return false; } var j = array.length - 1; while (array[j] <= array[i - 1]) { j--; } var temp = array[i - 1]; array[i - 1] = array[j]; array[j] = temp; j = array.length - 1; while (i < j) { temp = array[i]; array[i] = array[j]; array[j] = temp; i++; j--; } return array; }` `

` `1. Iterate over every character, we will get the last value i (starting from the first character) that satisfies the given condition S[i] < S[i + 1] 2. Now, we will get the last value j such that S[i] < S[j] 3. We now interchange S[i] and S[j]. And for every character from i+1 till the end, we sort the characters. ie, sort(S[i+1]..S[len(S) - 1])` `

nextperm（a，n）

` `1. find an index j such that a[j….n - 1] forms a monotonically decreasing sequence. 2. If j == 0 next perm not possible 3. Else 1. Reverse the array a[j…n - 1] 2. Binary search for index of a[j - 1] in a[j….n - 1] 3. Let i be the returned index 4. Increment i until a[j - 1] < a[i] 5. Swap a[j - 1] and a[i] O(n) for each permutation.` `

` `int main() { char str[100]; cin>>str; int len=strlen(len); int f=next_permutation(str,str+len); if(f>0) { print the string } else { cout<<"no answer"; } }` `