Algorithm: How to find the next lexicographic permutation

3r380. 3r3118. 3r380. 3r3118. Briefly describe what the lexicographic order is - sorting in alphabetical order. Those. the sequence of characters - AAA → AAB → AAC → AAD →

→ WWW - is sorted alphabetically (or in our case lexicographical) order. 3r380. 3r3118. 3r380. 3r3118. Imagine that you have a finite sequence of characters, for example, ? ? ? ? ? ? 0 and you need to find all possible permutations of these characters. The most intuitive, but also the greatest in complexity, is the recursive algorithm, when we select the first character from the sequence, then recursively select the second, third, etc., until all the characters from the sequence are selected. It is clear that the complexity of this algorithm is O (n!). 3r380. 3r3118. 3r380. 3r3118. But it turns out that the simplest algorithm for generating all permutations in the lexicographical order is to start with the smallest and repeatedly calculate the next permutation in place. Let's see how to do this. 3r380. 3r3118.

3r380. 3r3118. 3r380. 3r3118. Just as when calculating the next integer value, we should try to increase the right side of the sequence and leave the left side unchanged. 3r380. 3r3118. 3r380. 3r3118. As an example, take the above sequence - (? ? ? ? ? ? 0). To get the sequence above the original, it is enough to rearrange the first and second elements in places, but this is not necessary, since you can rearrange the second and the third and get a closer sequence in ascending order. which will lead us to the next closer permutation and so on. 3r380. 3r3118. 3r380. 3r3118. The best algorithm in this case is the following: 3r380. 3r3118. 3r380. 3r3118.

3r3118.

First of all, you must find the largest non-increasing suffix. In the above example, this will be - (? ? ? 0). If you try to make any permutation in this sequence, it will not be higher than the original one. 3r380. 3r3118. It is worth saying that you can find this sequence in O (n) time, looking through the sequence from left to right.

3r3118.

The next element from the suffix is a pivot point. In our case, it is 2. The turning point will always be less than the first element of the suffix. This means that in the suffix there will necessarily be an element that exceeds the turning point and if we change the turning point to the smallest element from the suffix that exceeds the reference element of the turning point - we will get a sequence that exceeds the original one - in our case it will be - ? ? 0). 3r380. 3r3118. Those. the result of this operation will be the minimum possible prefix in ascending order. 3r380. 3r3118.

3r3118.

And in the last step, we must sort the suffix in ascending order. Those. we get the lowest possible suffix. In our example, this will be (? ? ? 5) and the whole sequence will look like (? ? ? ? ? ? 5). 3r380. 3r3118.

3r3118.

3r380. 3r3118. 3r380. 3r3118. This value will be [b] following lexicographic rearrangement. 3r362. 3r380. 3r3118. 3r380. 3r3118. 3r380. 3r3118. 3r380. 3r3118. As for the practical application of the algorithm, I have never needed it for all the time of my work, but they didn’t think otherwise at an interview at Uber :)) 3r380. 3r3118. 3r380. 3r3118. 3r376. For simplicity, all the code will be written on Go and I think no one is difficult to translate it into any other programming language. [/i] 3r380. 3r3118. 3r380. 3r3118.` func NextPermutationAlgorithm (w string) string {`

3r3118. l: = len (w)

b: =[]byte (w)

3r3118. for i: = l-1; i> 0; i - {

if b[i-1]

It may be interesting