Exemplo n.º 1
0
        private void SortView_OnSort(object sender, SortEventArgs e)
        {
            Debug.WriteLine("Sort needed!");
            var sortedNumers = SortAlgorithm.Sort(e.NumbersToSort);

            SortView.DisplayResults(sortedNumers);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Destroy running math thread and clean up all pictures
            CleanUp();

            //There was dilhemma - to use new class instaces every time, or use singletons and static classes.
            //I choose new classes, actually there are already exists Globals class, so I don't want to create more global classes.

            SortView  v = new SortView();
            SortStore s = new SortStore();
            SortMath  m = new SortMath();

            if (cbSortSelector.Text == "Bubble sort")
            {
                Globals.arraySize = 250;
                Globals.frameSize = 4;
                //we need separate thread for non UI blocking sort
                Globals.MathThread = new Thread(() => { m.BubbleSort(s); });
            }


            if (cbSortSelector.Text == "Not so bubble sort")
            {
                Globals.arraySize  = 250;
                Globals.frameSize  = 4;
                Globals.MathThread = new Thread(() => { m.LooksLikeBubbleSort(s); });
            }

            if (cbSortSelector.Text == "Radix sort")
            {
                Globals.arraySize  = 300;
                Globals.frameSize  = 3;
                Globals.MathThread = new Thread(() => { m.RadixSort(s); });
            }

            if (cbSortSelector.Text == "Insertion sort")
            {
                Globals.arraySize  = 200;
                Globals.frameSize  = 4;
                Globals.MathThread = new Thread(() => { m.InsertionSort(s); });
            }

            if (cbSortSelector.Text == "Counting sort")
            {
                Globals.arraySize  = 400;
                Globals.frameSize  = 2;
                Globals.MathThread = new Thread(() => { m.CountingSort(s); });
            }


            if (cbSortSelector.Text == "Cocktail sort")
            {
                Globals.arraySize  = 200;
                Globals.frameSize  = 3;
                Globals.MathThread = new Thread(() => { m.CocktailSort(s); });
            }

            if (cbSortSelector.Text == "Shell sort")
            {
                Globals.arraySize  = 200;
                Globals.frameSize  = 3;
                Globals.MathThread = new Thread(() => { m.ShellSort(s); });
            }

            if (cbSortSelector.Text == "Gnome sort")
            {
                Globals.arraySize  = 200;
                Globals.frameSize  = 3;
                Globals.MathThread = new Thread(() => { m.GnomeSort(s); });
            }

            if (cbrand.Checked == true)
            {
                s.StoreInitRandom();
            }
            else
            {
                s.StoreInitSorted();
            }

            v.ViewInit(this, s);

            //connect store class with view class, while store class will be changed by math class
            s.ValueChanged  += v.ViewUpdate;
            s.ValueAccessed += v.ViewUpdate;


            if (Globals.MathThread != null)
            {
                Globals.MathThread.Start();
            }
        }