MergeSort数组 - 在C#中部分排序的解决方案

时间:2016-03-21 15:57:06

标签: c#

这是我的任务:  "程序还应显示阵列的部分排序解决方案。这些部分步骤应该清楚地显示算法如何对数组的元素进行排序。" 我可以显示未排序和排序的数组,但我需要知道如何显示(显示)所需的部分步骤?

我的代码是:

namespace MergeSort
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            String arrayLength;
            int num;

            // InputBox functionality used for user to enter the size of an array to be sorted.
            arrayLength = Interaction.InputBox("Enter a number that represent the length of array", "Enter Legth Of Array");
            try
            {
                while (!(int.TryParse(arrayLength, out num)))
                {
                    MessageBox.Show("Not a valid number!\r\nPlease try again!");
                    arrayLength = Interaction.InputBox("Enter a number that represent the length of array", "Enter Legth Of Array");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Value entered is not a valid format!", ex.Message);
            }
            int intLength = int.Parse(arrayLength);

            string[] stringArray = new string[intLength];
            int arrayPosition = 0;
            int positionValue = 1;

            //To display unsorted array
            txtOutput.Text += "Unsorted array: \r\n";
            foreach (string s in stringArray)
            {
                // InputBox functioanlity to prompt user to enter the text (string) into the arrary to be sorted.
                string value = Interaction.InputBox("Enter the string in the array to be sorted" + positionValue, "Enter the string ");
                stringArray[arrayPosition] = value.ToString();

                txtOutput.Text += value + "\t";
                arrayPosition++;
                positionValue++;
            }

            //To display sorted array
            txtOutput.Text += "\r\nSorted array: \r\n";
            mergeSort(stringArray, 0, stringArray.Length - 1);

            foreach (string s in stringArray)
            {
                txtOutput.Text += s + "\t";
            }
            txtOutput.Text = txtOutput.Text + "\r\n";


            //To display each step
            foreach (string s in stringArray)
            {
                txtOutput.Text += ("\r\nValue at position " + " " + Array.IndexOf(stringArray, s)+ "  " + "is" + "  " + s + "\r\n");
            }
        }

        public void mergeSort (string[] sortArray, int lower, int upper)
        {
            int middle;

            if (upper == lower)
                return;
            else
            {
                middle = (lower + upper) / 2;
                mergeSort(sortArray, lower, middle);
                mergeSort(sortArray, middle + 1, upper);
                Merge(sortArray, lower, middle + 1, upper);
            } 
        }

        public void Merge(string[] sortArray, int lower, int middle, int upper)
        {
            string[] temp = new string[sortArray.Length];
            int lowEnd = middle - 1;
            int low = lower;
            int n = upper - lower + 1;

            while ((lower <= lowEnd) && (middle <= upper))
            {
                if (sortArray[lower].CompareTo(sortArray[middle]) < 1)
                {
                    temp[low] = sortArray[lower];
                    low++;
                    lower++;
                }
                else
                {
                    temp[low] = sortArray[middle];
                    low++;
                    middle++;
                }
            }

            while (lower <= lowEnd)
            {
                temp[low] = sortArray[lower];
                low++;
                lower++;
            }

            while (middle <= upper)
            {
                temp[low] = sortArray[middle];
                low++;
                middle++;
            }

            for (int i = 0; i < n; i++)
            {
                sortArray[upper] = temp[upper];
                upper--;
            }
        }

    }
}

0 个答案:

没有答案