Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I want to implement merge sort with c#.net
But I have a problem
I wrote a program that does not run and I cannot find the problem

Please help me :((

textbox1
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = 0;

            List<int> lst = new List<int>();
            if (e.KeyChar == Convert.ToChar(Keys.Enter) && i < 10)
            {
                lst.Add(int.Parse(textBox1.Text));
                textBox1.Text = "";
                i++;

            }

        }



buttom1
XML
private void button1_Click(object sender, EventArgs e)
       {

           List<int> lst = new List<int>();
           int[] array = lst.ToArray();

           DoSort(array);


       }




call merge sort
private void DoSort(int[] array)
        {
            MergeSorter ms = new MergeSorter(array);
            Func<int[]> del = new Func<int[]>(ms.sortArray);
            del.BeginInvoke(new AsyncCallback(SortCallback), null);
        }
        private void SortCallback(IAsyncResult ar)
        {
            AsyncResult result = (AsyncResult)ar;
            Func<int[]> del = (Func<int[]>)result.AsyncDelegate;
            try
            {
                int[] array = del.EndInvoke(ar);
                this.textBox1.Invoke(new MethodInvoker(
                    delegate()
                    {
                        string[] sArray = array.ToList().ConvertAll<string>(Convert.ToString).ToArray();
                        textBox1.Text = string.Join(", ", sArray);
                    }));
            }
            catch (Exception ex)
            {
                this.textBox1.Invoke(new MethodInvoker(delegate()
                {
                    textBox1.Text = "Error sorting array: " + Environment.NewLine + ex.Message;
                }));
            }
        }


merge sort
    public class MergeSorter  {
    private int[] a;

    private int x;

private MergeSorter()
{
}
public MergeSorter(int[] array)
    :this()
{ this.a = array;
    this.x = array.Length; }
    public int[] sortArray()
    { q_sort(0, x - 1); return a; }
    private void q_sort(int left, int right)
    {
        int pivot, l_hold, r_hold;
        l_hold = left;
        r_hold = right;
        pivot = a[left];
        while
        (left < right)
        { while ((a[right] >= pivot) && (left < right))
        { right--; }
            if (left != right)
        { a[left] = a[right];
                left++; }
            while ((a[left] <= pivot) && (left < right))
            { left++; }
            if (left != right)
        { a[right] = a[left]; right--; }
        }
        a[left] = pivot;
        pivot = left;
        left = l_hold;
        right = r_hold;
        if (left < pivot)
        { q_sort(left, pivot - 1); }
        if (right > pivot)
        { q_sort(pivot + 1, right); }
    }
}
Posted
Updated 26-Apr-10 6:03am
v3

Why you are Creating List object every time when you keypress event..
Initialize only once, so you can add numbers to the list.

C#
List<int> lst = new List<int>();


and modify your events as,
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = 0;
            if (e.KeyChar == Convert.ToChar(Keys.Enter) && i < 10)
            {
                lst.Add(int.Parse(textBox1.Text));
                textBox1.Text = "";
                i++;
            }
        }


and

C#
private void button1_Click(object sender, EventArgs e)
       {
           int[] array = lst.ToArray();
           DoSort(array);
       }


it's working now...
Hope it helps..
 
Share this answer
 
v2
Thank you, but my problem still not resolved
Program is without error But does not answer
This is incorrect = List lst=new List();
because Error in the program will

This is correct = List<> lst = new List<>;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900