예제 #1
0
        private void RenderArray(StepChanges sc)
        {
            graphics.Clear(screen.BackColor);
            SolidBrush backgroundBrush = new SolidBrush(Color.LightBlue);
            Pen        pen             = new Pen(Color.Black);

            for (int i = 0; i < heightArray.Length; ++i)
            {
                graphics.FillRectangle(backgroundBrush, 10 + i * Params.rectWidth, 10,
                                       Params.rectWidth, heightArray[i]);
            }

            if (sc != null)
            {
                bool indOutOfBounds = sc.i >= heightArray.Length || sc.j >= heightArray.Length;
                if (!indOutOfBounds)
                {
                    Color      color          = sc.swaped ? Color.Red : Color.Green;
                    SolidBrush indicatorBrush = new SolidBrush(color);
                    graphics.FillRectangle(indicatorBrush, 10 + sc.i * Params.rectWidth, 10,
                                           Params.rectWidth, heightArray[sc.i]);
                    graphics.FillRectangle(indicatorBrush, 10 + sc.j * Params.rectWidth, 10,
                                           Params.rectWidth, heightArray[sc.j]);
                }
            }

            for (int i = 0; i < heightArray.Length; ++i)
            {
                graphics.DrawRectangle(pen, 10 + i * Params.rectWidth, 10,
                                       Params.rectWidth, heightArray[i]);
            }

            screen.Refresh();
        }
        public StepChanges Step()
        {
            StepChanges sc = null;

            if (Array[i] > max)
            {
                sc     = new StepChanges(i, maxInd, false);
                max    = Array[i];
                maxInd = i;
            }
            else
            {
                sc = new StepChanges(i, maxInd, true);
            }
            if (i == Array.Length - passNumber)
            {
                Helpers.Swap(Array, maxInd, i);
                sc = new StepChanges(i, maxInd, i != maxInd);
                ++passNumber;
                i      = 1;
                max    = Array[0];
                maxInd = 0;
                return(sc);
            }
            ++i;
            return(sc);
        }
예제 #3
0
        private StepChanges DownHeap()
        {
            StepChanges sc = null;

            if (child > i)
            {
                if (makeHeap)
                {
                    --parent;
                    GetChild(parent);
                }
                else
                {
                    GetChild(0);
                    goDownHeap = false;
                }
                return(null);
            }
            if (Array[(child - 1) / 2] < Array[child])
            {
                Helpers.Swap(Array, (child - 1) / 2, child);
                sc = new StepChanges((child - 1) / 2, child, true);
            }
            else
            {
                sc = new StepChanges((child - 1) / 2, child, false);
            }
            GetChild(child);
            return(sc);
        }
예제 #4
0
        public StepChanges Step()
        {
            StepChanges sc = null;

            if (takeNextGroup)
            {
                takeNextGroup = false;
                group         = queue.Dequeue();
                takeNextGroup = false;
                i             = group[0];
                j             = group[1] + 1;
                k             = 0;
                helpArray     = new int[group[2] - group[0] + 1];
            }
            while (i <= group[1] && j <= group[2])
            {
                if (Array[i] > Array[j])
                {
                    sc           = new StepChanges(i, j, true);
                    helpArray[k] = Array[j];
                    ++k;
                    ++j;
                    return(sc);
                }
                else
                {
                    sc           = new StepChanges(i, j, false);
                    helpArray[k] = Array[i];
                    ++k;
                    ++i;
                    return(sc);
                }
            }
            while (i <= group[1])
            {
                helpArray[k] = Array[i];
                ++k;
                ++i;
            }
            while (j <= group[2])
            {
                helpArray[k] = Array[j];
                ++k;
                ++j;
            }
            for (int m = group[0]; m <= group[2]; ++m)
            {
                Array[m] = helpArray[m - group[0]];
            }
            takeNextGroup = true;
            return(null);
        }
예제 #5
0
        private void Sort(object sender, EventArgs e)
        {
            ISortAlgorithm algorithm = algorithmDict[(string)algorithmList.SelectedItem];

            while (!algorithm.Finished())
            {
                StepChanges sc = algorithm.Step();
                RenderArray(sc);
                Thread.Sleep(Params.delay);
            }
            RenderArray(null);
            algorithm.Reset();
        }
예제 #6
0
        public StepChanges Step()
        {
            StepChanges sc = null;

            if (i >= Array.Length)
            {
                d /= 2;
                i  = d;
                j  = i - 1;;
                return(null);
            }
            if (d > 1)
            {
                if (Array[i - d] > Array[i])
                {
                    Helpers.Swap(Array, i - d, i);
                    sc = new StepChanges(i - d, i, true);
                }
                else
                {
                    sc = new StepChanges(i - d, i, false);
                }
                ++i;
                return(sc);
            }
            else
            {
                if (j < 0)
                {
                    ++i;
                    j = i - 1;
                    return(null);
                }
                if (Array[j] > Array[j + 1])
                {
                    Helpers.Swap(Array, j, j + 1);
                    --j;
                    return(new StepChanges(j + 1, j + 2, true));
                }
                else
                {
                    ++i;
                    j = i - 1;
                    return(new StepChanges(j + 1, j + 2, false));
                }
            }
        }
예제 #7
0
        public StepChanges Step()
        {
            StepChanges sc = null;

            if (takeNextGroup)
            {
                group         = queue.Dequeue();
                takeNextGroup = false;
                i             = group[0];
                border        = group[0];
            }
            if (i == group[1])
            {
                if (border - group[0] > 1)
                {
                    queue.Enqueue(new int[] { group[0], border - 1 });
                }
                if (group[1] - border > 1)
                {
                    queue.Enqueue(new int[] { border + 1, group[1] });
                }
                Helpers.Swap(Array, border, group[1]);
                takeNextGroup = true;
                return(null);
            }
            if (Array[i] < Array[group[1]])
            {
                Helpers.Swap(Array, i, border);
                sc = new StepChanges(i, group[1], true);
                ++border;
            }
            else
            {
                sc = new StepChanges(i, border, false);
            }
            ++i;
            return(sc);
        }