Find an algorithm (subroutine) that when called repeatedly with the same character-string variable that is initialized with n characters, all different, will cycle through all the permutations of those n characters, so that for example, when called 24 times with a string of length 4, will have cycled that string through all 24 permutations and returned it to its initial state.
Here's a solution which requires a separate tracking array:
AllPermutations(DataArray(), TrackingArray())(
/* Written by Erik Oosterwal */
i=0
While(TrackingArray(i)== i+1)(
i++
)
Mirror(DataArray(0), i+2)
for(j=0, j<i+1, j++)(
TrackingArray(j-1)=0
)
TrackingArray(i)++
return(DataArray(), TrackingArray())
)
|
Posted by Erik
on 2004-06-02 09:10:10 |