recursion - Implementing next_permutation in sml? -
how implement in sml? possible change inner for-loop recursive inner function?
void recursivepermute(char str[], int k) {   int j;   // base-case: fixed, print str.  if (k == strlen(str))      printf("%s\n", str);   else {       // try each letter in spot j.      (j=k; j<strlen(str); j++) {           // place next letter in spot k.          exchangecharacters(str, k, j);           // print spot k fixed.          recursivepermute(str, k+1);           // put old char back.          exchangecharacters(str, j, k);      }  }   }
you write like
val rec recursivepermute = fn (str, k) => ...   or
fun recursivepermute (str, k) = ...   you can check k , length of str like
if (string.size str = k) ... else ...   as inner-loop function, work in poor way, better else concerns representation of string. if want swap characters, work o(n) @ worst n - length of string (due need remove characters between them , put them back). generally, need use o(n^2) time quite ineffective.
Comments
Post a Comment