Esempio n. 1
0
        private void SelectionSort(int[] M)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.SelectionSort(AscRadioButton.Checked);
            step = 0;
            string str = " Dãy chưa sắp : ", strNew = "", strOld = "";

            for (int u = 0; u < M.Length; u++)
            {
                strOld += M[u].ToString() + " ";
            }
            str += strOld;
            saveQuaTrinh.Text += str;

            int    n  = M.Length;
            Status st = new Status();

            for (int i = 0; i < n - 1; i++)
            {
                int min = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (((AscRadioButton.Checked == true) && (M[min] > M[j])) || ((DescRadioButton.Checked == true) && (M[min] < M[j])))
                    {
                        min = j;
                    }
                }

                if (backgroundWorker1.CancellationPending)
                {
                    return;
                }
                if (min != i)
                {
                    int temp = M[i];
                    M[i]   = M[min];
                    M[min] = temp;
                    System.Threading.Thread.Sleep(15);
                    Swap(min, i);
                    step++;
                    str = " Bước " + step.ToString() + " : ";
                    for (int u = 0; u < M.Length; u++)
                    {
                        strNew += M[u].ToString() + " ";
                    }
                    str += strNew;
                    if (strNew != strOld)
                    {
                        saveQuaTrinh.Text += "\n" + str;
                    }
                    else
                    {
                        step--;
                    }
                    strOld = strNew;
                    strNew = "";
                }
            }
        }
Esempio n. 2
0
        private void InsertionSort(int[] M)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.InsertionSort(AscRadioButton.Checked);
            int    n = M.Length;
            int    x, temp;
            Status st = new Status();

            step = 0;
            string str = " Dãy chưa sắp : ", strNew = "", strOld = "";

            for (int u = 0; u < M.Length; u++)
            {
                strOld += M[u].ToString() + " ";
            }
            str += strOld;
            saveQuaTrinh.Text += str;
            for (int i = 1; i < n; i++)
            {
                x    = i - 1;
                temp = M[i];
                if (backgroundWorker1.CancellationPending)
                {
                    return;
                }
                while ((x >= 0) && (((AscRadioButton.Checked == true) && (M[x] > temp)) || ((DescRadioButton.Checked == true) && (M[x] < temp))))
                {
                    M[x + 1] = M[x];
                    System.Threading.Thread.Sleep(15);
                    SwapInsertion(x + 1, x);
                    x--;
                }

                M[x + 1] = temp;
                step++;
                str = " Bước " + step.ToString() + " : ";
                for (int u = 0; u < M.Length; u++)
                {
                    strNew += M[u].ToString() + " ";
                }
                str += strNew;
                if (strNew != strOld)
                {
                    saveQuaTrinh.Text += "\n" + str;
                }
                else
                {
                    step--;
                }
                strOld = strNew;
                strNew = "";
            }
        }
Esempio n. 3
0
        private void ShellSort(int[] M)
        {
            CodeSort.code = showCode;
            CodeSort.ShellSort(AscRadioButton.Checked);
            int i, n, gap, temp;
            int j;

            n    = M.Length;
            step = 0;
            string str = " Dãy chưa sắp : ";

            for (int u = 0; u < M.Length; u++)
            {
                str += M[u].ToString() + " ";
            }
            saveQuaTrinh.Text += str;
            Status st = new Status();

            for (gap = n / 2; gap > 0; gap = gap / 2)
            {
                step++;
                for (i = 0; i < n; i = i + gap)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }

                    temp = M[i];

                    for (j = i; j > 0 && M[j - gap] > temp; j = j - gap)

                    {
                        M[j] = M[j - gap];
                        Swap(j, j - gap);
                        str = " Bước " + step.ToString() + " : ";
                    }

                    M[j] = temp;
                }
                for (int u = 0; u < M.Length; u++)
                {
                    str += M[u].ToString() + " ";
                }
                saveQuaTrinh.Text += "\n" + str;
            }
        }
Esempio n. 4
0
        private void BubbleSort(int[] M)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.BubbleSort(AscRadioButton.Checked);

            int i, j;

            step = 0;
            string str = " Dãy chưa sắp : ";

            for (int u = 0; u < M.Length; u++)
            {
                str += M[u].ToString() + " ";
            }
            saveQuaTrinh.Text += str;
            int    n  = M.Length;
            Status st = new Status();

            for (i = 0; i < n; i++)
            {
                for (j = n - 1; j > i; j--)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                    if (((AscRadioButton.Checked == true) && (M[j] < M[j - 1])) || ((DescRadioButton.Checked == true) && (M[j] > M[j - 1])))
                    {
                        int tam = M[j];
                        M[j]     = M[j - 1];
                        M[j - 1] = tam;
                        System.Threading.Thread.Sleep(15);
                        Swap(j, j - 1);

                        step++;
                        str = " Bước " + step.ToString() + " : ";
                        for (int u = 0; u < M.Length; u++)
                        {
                            str += M[u].ToString() + " ";
                        }
                        saveQuaTrinh.Text += "\n" + str;
                    }
                }
            }
        }
Esempio n. 5
0
        void HeapSort(int[] M, int N)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.HeapSort(AscRadioButton.Checked);
            step = 0;
            string str = " Dãy chưa sắp : ";

            for (int u = 0; u < M.Length; u++)
            {
                str += M[u].ToString() + " ";
            }
            saveQuaTrinh.Text += str;

            CreateHeap(M, N);
            int r;

            r = N - 1;
            if (backgroundWorker1.CancellationPending)
            {
                return;
            }
            while (r > 0)
            {
                int temp = M[0];
                M[0] = M[r];
                M[r] = temp;

                System.Threading.Thread.Sleep(15);
                Swap(r, 0);
                step++;
                str = " Bước " + step.ToString() + " : ";
                for (int u = 0; u < M.Length; u++)
                {
                    str += M[u].ToString() + " ";
                }
                saveQuaTrinh.Text += "\n" + str;
                r--;
                if (r > 0)
                {
                    Shift(M, 0, r);
                }
            }
        }
Esempio n. 6
0
        private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e) //được thực thi khi gọi hàm RunWorkerSync()
        {
            if (listSort.selectedValue == "Bubble Sort")
            {
                BubbleSort(M);
            }
            else
            if (listSort.selectedValue == "Interchange Sort")
            {
                InterchangeSort(M);
            }
            else
            if (listSort.selectedValue == "Insertion Sort")
            {
                InsertionSort(M);
            }
            else
            if (listSort.selectedValue == "Selection Sort")
            {
                SelectionSort(M);
            }
            else
            if (listSort.selectedValue == "Binary Insertion Sort")
            {
                BinaryInsertionSort(M);
            }
            else
            if (listSort.selectedValue == "Shaker Sort")
            {
                ShakerSort(M);
            }
            else
            if (listSort.selectedValue == "Heap Sort")
            {
                HeapSort(M, M.Length);
            }
            else
            if (listSort.selectedValue == "Quick Sort")
            {
                CodeSort.idea = ideaSort;
                CodeSort.code = showCode;
                CodeSort.QuickSort(AscRadioButton.Checked);
                step = 0;
                string str = " Dãy chưa sắp : ";
                for (int u = 0; u < M.Length; u++)
                {
                    str += M[u].ToString() + " ";
                }
                saveQuaTrinh.Text += str;
                Quick_Sort(M, 0, M.Length - 1);
            }
            else
            if (listSort.selectedValue == "Shell Sort")
            {
                ShellSort(M);
            }
            else
            if (listSort.selectedValue == "Merge Sort")
            {
                CodeSort.idea = ideaSort;
                CodeSort.code = showCode;
                CodeSort.MergeSort(AscRadioButton.Checked);

                MergeSort1(M);
            }
            else
            {
                MessageBox.Show("Please Choose Sort !");
                return;
            }
        }
Esempio n. 7
0
        private void ShakerSort(int[] M)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.ShakerSort(AscRadioButton.Checked);
            step = 0;
            string str = " Dãy chưa sắp : ", strNew = "", strOld = "";

            for (int u = 0; u < M.Length; u++)
            {
                strOld += M[u].ToString() + " ";
            }
            str += strOld;
            saveQuaTrinh.Text += str;
            int j, left, right, k;
            int n = M.Length;

            left  = 0;
            right = n - 1;
            k     = n - 1;
            Status st = new Status();

            while (left < right)
            {
                for (j = right; j > left; j--)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                    if (((AscRadioButton.Checked == true) && (M[j] < M[j - 1])) || ((DescRadioButton.Checked == true) && (M[j] > M[j - 1])))
                    {
                        int temp = M[j];
                        M[j]     = M[j - 1];
                        M[j - 1] = temp;
                        System.Threading.Thread.Sleep(15);
                        Swap(j, j - 1);
                        k = j;

                        step++;
                        str = " Bước " + step.ToString() + " : ";
                        for (int u = 0; u < M.Length; u++)
                        {
                            strNew += M[u].ToString() + " ";
                        }
                        str += strNew;
                        if (strNew != strOld)
                        {
                            saveQuaTrinh.Text += "\n" + str;
                        }
                        else
                        {
                            step--;
                        }
                        strOld = strNew;
                        strNew = "";
                    }
                }
                left = k;
                for (j = left; j < right; j++)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                    if (((AscRadioButton.Checked == true) && (M[j + 1] < M[j])) || ((DescRadioButton.Checked == true) && (M[j + 1] > M[j])))
                    {
                        int temp = M[j];
                        M[j]     = M[j + 1];
                        M[j + 1] = temp;
                        System.Threading.Thread.Sleep(15);
                        Swap(j + 1, j);
                        k = j;

                        step++;
                        str = " Bước " + step.ToString() + " : ";
                        for (int u = 0; u < M.Length; u++)
                        {
                            strNew += M[u].ToString() + " ";
                        }
                        str += strNew;
                        if (strNew != strOld)
                        {
                            saveQuaTrinh.Text += "\n" + str;
                        }
                        else
                        {
                            step--;
                        }
                        strOld = strNew;
                        strNew = "";
                    }
                }
                right = k;
            }
        }
Esempio n. 8
0
        private void BinaryInsertionSort(int[] M)
        {
            CodeSort.idea = ideaSort;
            CodeSort.code = showCode;
            CodeSort.BinaryInsertionSort(AscRadioButton.Checked);
            step = 0;
            string str = " Dãy chưa sắp : ", strNew = "", strOld = "";

            for (int u = 0; u < M.Length; u++)
            {
                strOld += M[u].ToString() + " ";
            }
            str += strOld;
            saveQuaTrinh.Text += str;

            int    n = M.Length;
            int    x, left, right, m;
            Status st = new Status();

            for (int i = 1; i < n; i++)
            {
                x     = M[i];
                left  = 0;
                right = i - 1;
                while (left <= right)
                {
                    m = (left + right) / 2;
                    if ((((AscRadioButton.Checked == true) && (x < M[m])) || ((DescRadioButton.Checked == true) && (x > M[m]))))
                    {
                        right = m - 1;
                    }
                    else
                    {
                        left = m + 1;
                    }
                }

                for (int j = i - 1; j >= left; j--)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                    M[j + 1] = M[j];
                    System.Threading.Thread.Sleep(15);
                    SwapInsertion(j + 1, j);
                }
                M[left] = x;
                step++;
                str = " Bước " + step.ToString() + " : ";
                for (int u = 0; u < M.Length; u++)
                {
                    strNew += M[u].ToString() + " ";
                }
                str += strNew;
                if (strNew != strOld)
                {
                    saveQuaTrinh.Text += "\n" + str;
                }
                else
                {
                    step--;
                }
                strOld = strNew;
                strNew = "";
            }
        }