By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Simple Insertion sort method

P: 9
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:

Expand|Select|Wrap|Line Numbers
  1. label1.Text = Insertion.insert(textBox1.Text);
and here is the insertion class:

Expand|Select|Wrap|Line Numbers
  1. class Insertion
  2.     {
  3.         public static string insert(string x)
  4.         {
  5.  
  6.             for (int i = 2; i < x.Length; ++i)
  7.             {
  8.                 for (int j = i-1; j > 0 && x[j] < x[i]; --j)
  9.                 {
  10.                     x[j] = x[i];
  11.                 }
  12.             }
  13.             return x.ToString();
  14.         }
  15.     }
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?
Feb 14 '08 #1
Share this Question
Share on Google+
1 Reply


P: 9
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:

Expand|Select|Wrap|Line Numbers
  1. int[] str = new int[100]; 
  2.  
  3.             for (int k = 0; k < textBox1.Text.Length; k++)
  4.             {
  5.                 string z=textBox1.Text;
  6.  
  7.                 str[k] = textBox1.Text.IndexOf(z,k);  //this should put str[0]=textbox1.text[0] .. and so on
  8.             } 
but it doesn't work.
Feb 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.