"In place"
Can do better than previous algorithm
- for smallest permutation, numbers are increasing left to right
- for largest permutation, numbers are decreasing left to right
- can use this property to perform previous alg. in place.
improved algorithm
- array <- permute (array a) if for all i, a[i]>a[i+1] then this is the largest permutation already! find largest i, such that a[i]<a[i+1] (ie. search backwards: right to left) find largest j, such that a[i]<a[j] swap(a[i], a[j]) reverse(a[i+1 .. end]) return a