예제 #1
0
        static void CallPermutation()
        {
            string[] source = { "0", "1", "2", "3" };
            //string[] source = { "ant", "bat", "cow", "dog" };
            //StringPerm stringPerm = new StringPerm(a);

            for (StringPerm p = new StringPerm(source); p != null; p = p.Successor())
            {
                Console.WriteLine(p.ToString());
            }
        }
예제 #2
0
        public StringPerm Successor() // assumes no duplicate atoms
        {
            StringPerm result = new StringPerm(this.element);
            int        left, right;

            left = result.order - 2;  // Step #1 - Find left value
            while ((result.element[left].CompareTo(result.element[left + 1])) > 0 && (left >= 1))
            {
                --left;
            }
            if ((left == 0) && (this.element[left].CompareTo(this.element[left + 1])) > 0)
            {
                return(null);
            }

            right = result.order - 1;  // Step #2 - find right; first value > left
            while (result.element[left].CompareTo(result.element[right]) > 0)
            {
                --right;
            }

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

            result.element[left]  = result.element[right];
            result.element[right] = temp;

            int i = left + 1;              // Step #4 - reverse order the tail
            int j = result.order - 1;

            while (i < j)
            {
                temp = result.element[i];
                result.element[i++] = result.element[j];
                result.element[j--] = temp;
            }

            //return result;
            return(result);
        } // Successor()