0

I am playing around with sorting arrays and I figured out how to MergeSort an int array. But I cant figure out to MergeSort a string array. Sorting string array is easy enough when normal sorting but Merge Sort is different. The code I have done so far is below and is working on int arrays.

public int number = 1;
public void mergeSort(int[] 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(int[] 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] <= sortArray[middle])
            {
                temp[low] = sortArray[lower].ToString();
                low++;
                lower++;
            }
            else
            {
                temp[low] = sortArray[middle].ToString();
                low++;
                middle++;
            }
        }
        while (lower <= lowEnd)
        {
            temp[low] = sortArray[lower].ToString();
            low++;
            lower++;
        }
        while (middle <= upper)
        {
            temp[low] = sortArray[middle].ToString();
            low++;
            middle++;
        }
        for (int i = 0; i < n; i++)
        {
            sortArray[upper] = Int32.Parse(temp[upper]);
            upper--;
        }
    }
private void btnExecute_Click(object sender, EventArgs e)
    {
        String arraylength;
        int num;
        arraylength = Microsoft.VisualBasic.Interaction.InputBox("Enter a number to determine the length of the array", "Enter Number");
        try
        {
            while (!(int.TryParse(arraylength, out num)))
            {
                MessageBox.Show("Not a valid number, try again.");
                arraylength = Microsoft.VisualBasic.Interaction.InputBox("Enter a number a to determine the length of the array", "Enter Number");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Value entered is not in a valid format");
        }
        int intlength = Int32.Parse(arraylength);

        string[] stringsArray = new string[intlength];


        int arraypos = 0;
        // For display purposes
        int positionvalue = 1;
        txtOutput.Text += "Unsorted array: \r\n";
        foreach (string s in stringsArray)
        {
            string arrayvalue = Microsoft.VisualBasic.Interaction.InputBox("Enter a number for array value" + positionvalue, "Enter Number");
            string arrayvalues = arrayvalue;
            stringsArray[arraypos] = arrayvalues.ToString();

            txtOutput.Text += arrayvalues + "\t";
            arraypos++;
            positionvalue++;
        }
        mergeSort(stringsArray, 0, stringsArray.Length - 1);

        txtOutput.Text += "\r\nSorted array: \r\n";
        foreach (string i in stringsArray)
        {
            txtOutput.Text += i + "\t";
        }

    }
2
  • in C# you can compare two Strings like int and use above code for your array of Strings. Commented Oct 11, 2015 at 5:21
  • Instead of using a <= b, you can use IComparer<T>.Compare(a, b) <= 0. You can use default comparer for type T: Comparer<T>.Default, or you can allow caller to specify its own IComparer<T> instance. Commented Oct 11, 2015 at 6:44

1 Answer 1

0

I think the main problem you are facing while using your sort for string is the below line

if (sortArray[lower] <= sortArray[middle])

Because you cannot use <= on two strings. Rather, what you can do is, you can use the string.CompareTo() method. See the MSDN doc on IComparable for string.

Since you can use that in your code, you can change your code to be like

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

You might need to adjust your last loop and few other minor stuffs.

for (int i = 0; i < n; i++)
{
    sortArray[upper] = temp[upper]; //sortArray is string[]
    upper--;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.