Quicksort index out of range exception
<p>I'm trying a different variant of <strong>QuickSort</strong> where I sort two <code>ListBoxes</code>.</p>

<p>here is my code:</p>

<pre><code>class cS
{
    public static int[] mQS(int[] iar)
    {
        //divide and conquer
        if (iar.Length &lt; 3)
        {
            if (iar.Length == 2)
            {
                if (iar[0] &gt; iar[1])
                {
                    //wissel
                    int T = iar[1];
                    iar[1] = iar[0];
                    iar[0] = T;
                }
            }
        }
        else
        {
            int size = iar.Length;
            int sum = 0;
            int average = 0;
            for (int i = 0; i &lt; size; i++)
            {
                sum += iar[i];
            }
            average = sum / size; 

            int lk = 1, lg = 1;
            for (int i = 0; i &lt; size; i++)
            {
                if(iar[i]&lt;=average)
                {
                    lk++;
                }
            else
            {
                lg++;
            }
        }

        int[] ak = new int[lk];
        int[] ag = new int[lg];
        lk--; lg--;

        for (int i = 0; i &lt; average; i++)
        {
            if (iar[i] &lt;= average)
            {
                ak[lk]=iar[i];
                lk--; 
            }
            else
            {
                ag[lg] = iar[i];
                lg--;
            }
        }

        if (!Allegelijk(ak)) {ak= mQS(ak);}
        if(!Allegelijk(ag)) {ag = mQS(ag);}
        // plak weer aan elkaar in iar
        for (int i = 0; i &lt; ak.Length; i++)
        {
            iar[i] = ak[i];
        }
        for (int i = 0; i &lt; ag.Length; i++)
        {
            iar[i + ak.Length] = ag[i];
        }
    }
    return iar;
}

private static  bool allEqual(int[] ar)
{
    bool bG = true;
    for (int i = 1; i &lt; ar.Length; i++)
    {
        if (ar[i] != ar[0])
        {
            bG = false;
            break;
        }
     }
     return bG;
 }

}
</code></pre>

<p>}</p>

<p>But when running I get this error:</p>

<pre><code>index out of range exception at the line :

  if (iar[i] &lt;= average&gt;
</code></pre>

<p>Can anyone explain what is happening and how to fix it?</p>
