A common application for recursion

Hi,

A common application for recursion is the problem of generating all possible permutations of a set of symbols. For the se consisting of symbols A,B and C there exists six permutations - namely, ABC, ACB, BAC, BCA, CBA and CAB. The set of permutations of N symbols is generated by taking each symbol in turn and prefixing it to all the permutations which result from the remaining N-1 symbols. It is therefore, possible to specify the permutations of a set of symbols in terms of permutations of a small set of symbols. How to write a recursive function program for generating all possible permutations of a set of symbols using C#.

This is very urgent, Plz help

shine




Answer this question

A common application for recursion

  • GregUzelac

    Probably he'll fire me out.

  • Daniel Bogdan

    No, it's not. Plz help

  • Fang Wang - MSFT

    give him my name. . . I am always looking for work. . . I also know how to google

    permutation +recursion +algorithm



  • Anonymous1980

    Here is the class. A eral world example searcher for all records with the name "John Pat Doe" and you want to ensure that you get all of the records regardless how the name is in the record.

    public class Permutation : IDisposable

    {

    private int[] data = null;

    private int order = 0;

    private Permutation(int y)

    {

    this.data = new intYes;

    for (int x = 0; x < y; ++x)

    {

    this.data[x] = x;

    }

    this.order = y;

    }

    public Permutation()

    {

    }

    public static object[] Permutate(string arg, string delimiter)

    {

    string result = string.Empty;

    string[] s;

    object[] res;

    string ss;

    System.Collections.ArrayList list = new System.Collections.ArrayList();

    s = (arg).Split(delimiter.ToCharArray());

    if (s.Length == 1)

    {

    list.Add(s[0]);

    }

    else

    {

    res = new object[s.Length];

    Permutation perm = new Permutation(s.Length);

    while (perm != null)

    {

    res = perm.ApplyTo(s);

    ss = string.Empty;

    for (int x = 0; x < s.Length; x++)

    {

    ss += res[x].ToString() + delimiter;

    }

    list.Add(ss.TrimEnd(delimiter.ToCharArray()));

    perm = perm.Successor();

    }

    }

    return list.ToArray();

    }

    private object[] ApplyTo(object[] arr)

    {

    if (arr.Length != this.order)

    return null;

    object[] result = new object[arr.Length];

    for (int i = 0; i < result.Length; ++i)

    {

    result[x] = arr[this.data[x]];

    }

    return result;

    } // ApplyTo()

    private Permutation Successor()

    {

    Permutation result = new Permutation(this.order);

    int left, right;

    for (int k = 0; k < result.order; ++k) // Step #0 - copy current data into result

    {

    result.data[k] = this.data[k];

    }

    left = result.order - 2; // Step #1 - Find left value

    while ((result.data[left] > result.data[left+1]) && (left >= 1))

    {

    --left;

    }

    if ((left == 0) && (this.data[left] > this.data[left+1]))

    return null;

    right = result.order - 1; // Step #2 - find right; first value > left

    while (result.data[left] > result.data[right])

    {

    --right;

    }

    int temp = result.data[left]; // Step #3 - swap [left] and [right]

    result.data[left] = result.data[right];

    result.data[right] = temp;

    int x = left + 1; // Step #4 - order the tail

    int j = result.order - 1;

    while (i < j)

    {

    temp = result.data[x];

    result.data[x++] = result.data[j];

    result.data[j--] = temp;

    }

    return result;

    } // Successor()

    #region IDisposable Members

    public void Dispose()

    {

    // TODO: Add Permutation.Dispose implementation

    }

    #endregion

    } // class Permutation


  • skylar119506

    is this a homework assignment

  • shuttler

    My project manager gave me this. He told me to create a c# function for that and after it has to implement it in a real world. But no idea about the real world right now. Can u help me

  • haiaw

    Hmm...looks a lot like an assignment I was given back when I was in College.

    Perhaps if you can tell us more about the 'real world' problem that you are trying to apply this to, we can give you some help.



  • A common application for recursion