Simple Insertion sort method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AhmedGY
    New Member
    • Jan 2008
    • 9

    Simple Insertion sort method

    Hi, am trying to build an app that uses the insertion sort method to sort numbers entered in a textbox and display them sorted in a label, so i wrote this inside the sort button click event:

    Code:
    label1.Text = Insertion.insert(textBox1.Text);
    and here is the insertion class:

    Code:
    class Insertion
        {
            public static string insert(string x)
            {
                
                for (int i = 2; i < x.Length; ++i)
                {
                    for (int j = i-1; j > 0 && x[j] < x[i]; --j)
                    {
                        x[j] = x[i];
                    }
                }
                return x.ToString();
            }
        }
    Although it looks logical to me, it doesn't work and gives an error: Property or indexer 'string.this[int]' cannot be assigned to -- it is read only.

    How to get this workin?
  • AhmedGY
    New Member
    • Jan 2008
    • 9

    #2
    Any help?

    I tried to use int instead of string, however i don't know how to put string elements inside an array of integers.

    Did it this way:

    Code:
    int[] str = new int[100]; 
    
                for (int k = 0; k < textBox1.Text.Length; k++)
                {
                    string z=textBox1.Text;
    
                    str[k] = textBox1.Text.IndexOf(z,k);  //this should put str[0]=textbox1.text[0] .. and so on
                }
    but it doesn't work.

    Comment

    Working...