Ejemplo n.º 1
0
        public void Start()
        {
            const int measures = 50;

            Graph.Data cb  = new Graph.Data("Array B");
            Graph.Data cl  = new Graph.Data("List L");
            Graph.Data ctr = new Graph.Data("Tree TR");
            Graph.Data ctb = new Graph.Data("Tree TB");
            Graph.Data sb  = new Graph.Data("Array B");
            Graph.Data sbb = new Graph.Data("Array B (dividing by half)");
            Graph.Data sl  = new Graph.Data("List L");
            Graph.Data str = new Graph.Data("Tree TR");
            Graph.Data stb = new Graph.Data("Tree TB");
            Graph.Data htr = new Graph.Data("Tree TR");
            Graph.Data htb = new Graph.Data("Tree TB");

            Stopwatch stopwatch = new Stopwatch();

            for (int i = 0; i < measures; i++)
            {
                int n = (i + 1) * 500;

                int[] a = GenerateArray(n);

                //Array B
                stopwatch.Start();
                int[] b = new int[n];
                for (int j = 0; j < n; j++)
                {
                    b[j] = a[j];
                }
                (new QuickSort()).Sort(b);
                stopwatch.Stop();
                cb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    Find(b, a[j]);
                }
                stopwatch.Stop();
                sb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    FindByHalf(b, a[j]);
                }
                stopwatch.Stop();
                sbb.AddPoint(n, stopwatch.ElapsedMilliseconds);

                //List L
                stopwatch.Restart();
                List <int> l = new List <int>();
                for (int j = 0; j < n; j++)
                {
                    l.Add(a[j]);
                }
                stopwatch.Stop();
                cl.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    l.IndexOf(a[j]);
                }
                stopwatch.Stop();
                sl.AddPoint(n, stopwatch.ElapsedMilliseconds);

                //Tree TR
                stopwatch.Restart();
                BinarySearchTree <int, int> tr = new BinarySearchTree <int, int>();
                for (int j = 0; j < n; j++)
                {
                    tr.Add(a[j], a[j]);
                }
                stopwatch.Stop();
                ctr.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    tr.GetElement(a[j]);
                }
                stopwatch.Stop();
                str.AddPoint(n, stopwatch.ElapsedMilliseconds);
                htr.AddPoint(n, tr.GetHeight());

                //Tree TB
                int[] temp = Median(b);
                stopwatch.Restart();
                BinarySearchTree <int, int> tb = new BinarySearchTree <int, int>();
                for (int j = 0; j < n; j++)
                {
                    tb.Add(temp[j], temp[j]);
                }
                stopwatch.Stop();
                ctb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    tb.GetElement(a[j]);
                }
                stopwatch.Stop();
                stb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                htb.AddPoint(n, tb.GetHeight());

                Console.WriteLine((i + 1) + "/" + measures + " completed.");
            }

            string path = @".\Graphs";

            Graph creationGraph = new Graph("Creation Time", "Number of elements", "Time[ms]");

            creationGraph.AddData(cb);
            creationGraph.AddData(cl);
            creationGraph.AddData(ctr);
            creationGraph.AddData(ctb);
            creationGraph.StartWithNewThread();
            creationGraph.WriteToFile(path);

            Graph searchGraph = new Graph("Search Time", "Number of elements", "Time[ms]");

            searchGraph.AddData(sb);
            searchGraph.AddData(sbb);
            searchGraph.AddData(sl);
            searchGraph.AddData(str);
            searchGraph.AddData(stb);
            searchGraph.StartWithNewThread();
            searchGraph.WriteToFile(path);

            Graph heightGraph = new Graph("Height", "Number of elements", "Height");

            heightGraph.AddData(htr);
            heightGraph.AddData(htb);
            heightGraph.StartWithNewThread();
            heightGraph.WriteToFile(path);
        }