Esempio n. 1
0
        private void Task1()
        {
            Console.WriteLine("Insert number of elements");
            int n = InputParameter();

            Console.WriteLine("Insert maximum value of elements");
            int max = InputParameter();

            Element[] elements = GenerateElements(n, max);

            SortingAlgorithm s = InputAlgorithm();

            if (s.IsInPlace())
            {
                Visualization g = new Visualization(elements);
                g.StartWithNewThread();
            }
            s.Sort(elements);
            Console.WriteLine(s.GetName() + " has finished");
            if (s.Stable)
            {
                Console.WriteLine(s.GetName() + " is stable");
            }
            else
            {
                Console.WriteLine(s.GetName() + " is not stable");
            }
            Console.WriteLine("Time: " + s.GetElapsedTime());
        }
Esempio n. 2
0
        private SortingAlgorithm InputAlgorithm()
        {
            SortingAlgorithm s = null;

            Console.WriteLine("Choose a sorting algorithm");
            var types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(x => x.BaseType == typeof(SortingAlgorithm));
            int i     = 1;

            foreach (var x in types)
            {
                SortingAlgorithm temp = (SortingAlgorithm)Activator.CreateInstance(x);
                Console.WriteLine(i + ": " + temp.GetName());
                i++;
            }
            while (s == null)
            {
                int input = InputParameter();
                i = 1;
                foreach (var x in types)
                {
                    if (i == input)
                    {
                        s = (SortingAlgorithm)Activator.CreateInstance(x);
                    }
                    i++;
                }
                if (s == null)
                {
                    Console.WriteLine("There is no algorithm with such index");
                }
            }
            return(s);
        }
Esempio n. 3
0
        private void Task2()
        {
            Console.WriteLine("Insert the first number of elements");
            int   nm    = InputParameter();
            Graph g     = new Graph("Number of elements", "Time[s]");
            var   types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(x => x.BaseType == typeof(SortingAlgorithm));
            Table table = new Table(3, 21);

            table.StartWithNewThread();
            foreach (var x in types)
            {
                SortingAlgorithm s   = (SortingAlgorithm)Activator.CreateInstance(x);
                Table.Tab        tab = new Table.Tab(s.GetName());
                table.AddTab(tab);
                tab.AddLine(new string[] { "Number of elements", "Time (in seconds)", "Number of iterations" });
                Graph.Data pointData = new Graph.Data(s.GetName());
                for (int i = 0; i < 20; i++)
                {
                    int       n        = nm * (i + 1);
                    Element[] elements = GenerateElements(n, n);
                    s.Sort(elements);
                    pointData.AddPoint(n, ((float)s.GetElapsedTime() / 1000));
                    string[] line = { n.ToString(), ((float)s.GetElapsedTime() / 1000).ToString("0.00"), s.GetIterationCount().ToString() };
                    tab.AddLine(line);
                }
                g.AddData(pointData);
            }
            g.StartWithNewThread();
        }
Esempio n. 4
0
        private void Task1()
        {
            Console.WriteLine("Insert number of elements");
            int n = InputParameter();

            Console.WriteLine("Insert maximum value of elements");
            int max = InputParameter();

            Element[] elements = GenerateElements(n, max);

            SortingAlgorithm s = InputAlgorithm();
            Visualization    g = new Visualization(elements);

            g.StartWithNewThread();
            s.Sort(elements);
            Console.WriteLine("Time: " + s.GetElapsedTime());
        }
Esempio n. 5
0
 /// <summary>
 /// Method that allows to set a new sorting strategy on runtime
 /// </summary>
 /// <param name="sortingAlgorithm"> New sorting strategy </param>
 public void SetAlgorithm(SortingAlgorithm <T> sortingAlgorithm)
 {
     _sortingAlgorithm = sortingAlgorithm ?? throw new ArgumentNullException();
 }
Esempio n. 6
0
 /// <summary>
 /// Constructor for initializing a sorting strategy
 /// </summary>
 /// <param name="sortingAlgorithm"> Concrete sorting strategy </param>
 public SortingContext(SortingAlgorithm <T> sortingAlgorithm)
 {
     _sortingAlgorithm = sortingAlgorithm ?? throw new ArgumentNullException();
 }