Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site masscomp.UUCP Path: utzoo!watmath!clyde!bonnie!masscomp!lip From: lip@masscomp.UUCP (John Lipinski) Newsgroups: net.lang.c Subject: Re: Need C Algorithm Message-ID: <174@masscomp.UUCP> Date: Thu, 13-Dec-84 10:53:14 EST Article-I.D.: masscomp.174 Posted: Thu Dec 13 10:53:14 1984 Date-Received: Fri, 14-Dec-84 06:08:44 EST References: <1289@eosp1.UUCP> Reply-To: lip@masscomp.UUCP (John Lipinski) Distribution: net.unix-wizards,net.unix,net.lang,net.puzzle Organization: Masscomp - Westford, MA Lines: 51 Summary: For Warren Lobel at Exxon Office Systems, who requested a solution to the permutation problem. The following C program takes a string as an argument and prints out the list of permutations. It is a very interesting algorithm. - John Lipinski ------------------------------------------------------------------------ /* permutate: prints a list of all possible permutations of a string. */ /* usage: permutate string */ main(argc,argv) int argc; char **argv; { char *word, *malloc(); if (argc < 2) exit(1); word = malloc(strlen(argv[1]) + 2); strcpy(word,argv[1]); permutate(word,strlen(word)-1); } permutate(word,right) char word[]; int right; { int left; if (right > 0) { for(left = right; left >= 0; left--) { swap(word,left,right); permutate(word,right-1); swap(word,left,right); } return; } else printf("%s\n",word); } swap(s,l,r) char s[]; int l, r; { char c; c = s[l]; s[l] = s[r]; s[r] = c; }