-1

I'm having a problem with getting this code (from Rosetta Code) to work as part of a greater Windows Form Project:

It's a plain old insertion sort. Where it's going wrong is the bit involving the second for loop (The numbers generate just fine), in the sorting part of it. To me within a couple of iterations of the loop then j will be into negative figures and whilst other languages such as Javascript and Pascal don't seem to mind this...C# isn't happy.

        private void button3_Click(object sender, EventArgs e)
        {

            int i, j, k;
            int[] a = new int[12];
            Random randomObject = new Random();
            ClearOutputs(); // this is an event which just clears the 
               text 
            from the text boxes.
            //Generate some random numbers 
            for (i = 0; i < a.Length; i++)
            {
                a[i] = randomObject.Next(1, 1000);
                textBox1.AppendText(a[i].ToString() + "\n");
            }

            for (i = 1; i <= a.Length; i++)
            {
                k = a[i];
   //*******************
                for (j = i; j > 0 & k < a[j - 1]; j--)
                {
                    a[j] = a[j - 1];
                    a[j] = k;
                }
            }
    // ***************
            //Display them...it never executes this part.
            for (i = 0; i < a.Length; i++)
            {
                textBox2.AppendText(a[i].ToString() + "\n");
            }
        }

The longer term fix would be for me to understand the coding of the algorithm...and then fix it for myself, but if anyone could point me in the right direction...I've tried setting the 'for' loop at a higher initial value and yet still got the same "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" Any help would be greatly appreciated.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Mark Wild
  • 1
  • 2
  • 2
    `j > 0 & k < a[j - 1]` should be `j > 0 && k < a[j - 1]` - the first one is a binary AND, the second one a logical/boolean AND – UnholySheep Nov 14 '17 at 17:21
  • Also `i <= a.Length` has to be `i < a.Length` - otherwise you will go out of bounds on `k = a[i];` – UnholySheep Nov 14 '17 at 17:27
  • Thanks :) Changing to ' i – Mark Wild Nov 14 '17 at 17:37
  • @UnholySheep: _"the first one is a binary AND"_ -- incorrect. the code would not compile if `&` was being treated as a binary AND, because the expression `j > (0 & k) < a[j - 1]` is not valid. In this context, the comparison operators have precedence, and the `&` is simply the non-short-circuiting logical AND. – Peter Duniho Nov 15 '17 at 01:20
  • @PeterDuniho I agree my phrasing was incorrect, but according to the [specification](https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#operator-precedence-and-associativity) relational operators always have higher precedence than both `&` and `&&` or am I missing some crucial detail? – UnholySheep Nov 15 '17 at 07:36
  • @UnholySheep: _"according to the specification relational operators always have higher precedence than both & and &&"_ -- yes, they do, which is why the `&` here is _not_ the binary (i.e. bitwise) AND operator. Because `<` and `>` have higher precedence, the arguments of `&` are `bool` values, not `int`, and hence the logical AND operator is selected, not the binary AND operator. Put another way, the specification you refer to explains exactly why your comment is incorrect. – Peter Duniho Nov 15 '17 at 08:08

1 Answers1

0

Following the provided Rosetta Code algorithm, you should change the second "for" block to:

for (i = 1; i < a.Length; i++)
{
    k = a[i];
    for (j = i - 1; j >= 0 && a[j] > k; j--)
    {
        a[j + 1] = a[j];
    }
    a[j + 1] = k;
}
ceferrari
  • 1,597
  • 1
  • 20
  • 25
  • Thanks for this :) Wow my coding was way off wasn't it! My translation of the Rosetta Code version was not like for like, I had attempted to blend the Delphi and Fortran versions as the C# didn't make enough sense to me - I'm quite new to that world of terrifyingly obscure OOP coding. – Mark Wild Nov 15 '17 at 09:06