private void heapify(int n, int i)
        {
            int largest = i;
            int l       = 2 * i + 1;
            int r       = 2 * i + 2;

            intButtons[i].BackColor = Color.Yellow;

            if (l < n && this[l] > this[largest])
            {
                largest = l;
            }
            if (r < n && this[r] > this[largest])
            {
                largest = r;
            }


            if (largest != i)
            {
                intButtons[largest].BackColor = Color.Yellow;
                Thread.Sleep(ThongSo.Sleep);
                IntButton.Swap(ref intButtons[i], ref intButtons[largest]);
                Thread.Sleep(ThongSo.Sleep);
                intButtons[i].BackColor       = ThongSo.clIntButton;
                intButtons[largest].BackColor = ThongSo.clIntButton;
                heapify(n, largest);
            }
            intButtons[i].BackColor = ThongSo.clIntButton;
        }
        private int partition(int low, int high)
        {
            int pivot = this[high].Value; // pivot

            intButtons[high].BackColor = Color.Green;
            int i = (low - 1); // Index of smaller element

            for (int j = low; j <= high - 1; j++)
            {
                intButtons[i + 1].BackColor = Color.Aqua;
                Thread.Sleep(ThongSo.Sleep);

                // If current element is smaller than the pivot
                if (this[j] < pivot)
                {
                    i++; // increment index of smaller element
                    intButtons[j].BackColor = Color.Gold;
                    intButtons[i].BackColor = Color.Gold;


                    //intButtons[i ].BackColor = ThongSo.clIntButton;
                    IntButton.Swap(ref intButtons[i], ref intButtons[j]);
                    Thread.Sleep(ThongSo.Sleep);
                    if (intButtons[j].Value < pivot)
                    {
                        intButtons[j].BackColor = Color.Blue;
                    }
                    else
                    {
                        intButtons[j].BackColor = Color.Red;
                    }
                    intButtons[i].BackColor = Color.Blue;
                }
                else
                {
                    intButtons[j].BackColor = Color.Red;
                }
            }
            intButtons[i + 1].BackColor = Color.Gold;
            intButtons[high].BackColor  = Color.Gold;
            Thread.Sleep(ThongSo.Sleep);

            IntButton.Swap(ref intButtons[i + 1], ref intButtons[high]);
            for (int k = low; k <= high; k++)
            {
                intButtons[k].BackColor = ThongSo.clIntButton;
            }

            return(i + 1);
        }
        public void selectionSort()
        {
            int i, j, min_idx;


            for (i = 0; i < intButtons.Length - 1; i++)
            {
                min_idx = i;

                for (j = i + 1; j < intButtons.Length; j++)
                {
                    intButtons[j].BackColor = Color.Aqua;
                    intButtons[i].BackColor = Color.Green;
                    Thread.Sleep(ThongSo.Sleep);

                    if (this[j] < this[min_idx])
                    {
                        if (min_idx != i)
                        {
                            intButtons[min_idx].BackColor = ThongSo.clIntButton;
                        }
                        min_idx = j;
                        intButtons[min_idx].BackColor = Color.Red;
                        Thread.Sleep(ThongSo.Sleep);
                    }
                    else
                    {
                        intButtons[j].BackColor = ThongSo.clIntButton;
                    }
                }
                intButtons[min_idx].BackColor = Color.Blue;
                intButtons[i].BackColor       = Color.Blue;
                Thread.Sleep(ThongSo.Sleep);
                IntButton.Swap(ref intButtons[min_idx], ref intButtons[i]);
                Thread.Sleep(ThongSo.Sleep);
                intButtons[min_idx].BackColor = ThongSo.clIntButton;
                intButtons[i].BackColor       = Color.Yellow;
            }

            intButtons[intButtons.Length - 1].BackColor = Color.Yellow;
            ThongSo.IsAlive = false;
        }
        private void heapSort(int n)
        {
            for (int i = n / 2 - 1; i >= 0; i--)
            {
                heapify(n, i);
            }

            for (int i = n - 1; i >= 0; i--)
            {
                intButtons[i].BackColor = Color.Yellow;
                intButtons[0].BackColor = Color.Yellow;
                Thread.Sleep(ThongSo.Sleep);
                IntButton.Swap(ref intButtons[0], ref intButtons[i]);
                Thread.Sleep(ThongSo.Sleep);
                intButtons[i].BackColor = ThongSo.clIntButton;
                intButtons[0].BackColor = ThongSo.clIntButton;
                heapify(i, 0);
            }
            ThongSo.IsAlive = false;
        }
        public void BubbleSort()
        {
            for (int i = 0; i < intButtons.Length; i++)
            {
                for (int j = 0; j < intButtons.Length - i - 1; j++)
                {
                    intButtons[j].BackColor = Color.Green;
                    Thread.Sleep(ThongSo.Sleep);

                    if (this[j] > this[j + 1])
                    {
                        intButtons[j + 1].BackColor = Color.Green;
                        Thread.Sleep(ThongSo.Sleep);
                        IntButton.Swap(ref intButtons[j], ref intButtons[j + 1]);

                        Thread.Sleep(ThongSo.Sleep);
                    }
                    intButtons[j].BackColor     = Color.White;
                    intButtons[j + 1].BackColor = Color.Yellow;
                }
            }
            intButtons[0].BackColor = Color.Yellow;
            ThongSo.IsAlive         = false;
        }