Q:

Write a C# Sharp program to sort a list of elements using Permutation sort ?

0

Write a C# Sharp program to sort a list of elements using Permutation sort ?
Permutation sort, proceeds by generating the possible permutations of the input array/list until discovering the sorted one.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    private static void Swap(ref char a, ref char b)
    {
        if (a == b) return;
        a ^= b;
        b ^= a;
        a ^= b;
    }
    public static void GetPer(char[] list)
    {
        int x = list.Length - 1;
        GetPer(list, 0, x);
    }
    private static void GetPer(char[] list, int k, int m)
    {
        if (k == m)
        {
            {
                Console.WriteLine(list);
            }          
        }
        else
            for (int i = k; i <= m; i++)
            {
                   Swap(ref list[k], ref list[i]);
                   GetPer(list, k + 1, m);
                   Swap(ref list[k], ref list[i]);
            }
    }

    static void Main()
    {
        string str = "ABC";
        char[] arr = str.ToCharArray();
        GetPer(arr);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now