recursion problem

This is a discussion on recursion problem within the C++ Programming forums, part of the General Programming Boards category; I am working on a project and I need some help on one part. How do you write a recursive ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    recursion problem

    I am working on a project and I need some help on one part.

    How do you write a recursive function to rearrange strings (permutations) in all possible combinations.

    Like if the input was ABC:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    would you switch letters or is there an easier way...

    Thanks,

    Dustin

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Something based on this perhaps

    "A" + permute("BC")
    "B" + permute("AC")
    "C" + permute("AB")

    Basically, put each char in the left position in turn, and permute the rest.

    permute stops being recursive when you do permute("")
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 02:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By ender in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21