Какая то ошибка делал сортировщик массива

class Algaritm
{
    public void SortArr(ref int[] mas)
    {
        bool R = false;
        int num = -199999999;
        int EndMas = mas.Length;
        int[] mas2 = new int[mas.Length];
        while (!R)
        {
            if (EndMas > 0)
            {
                for (int i = 0; i <= EndMas; i++)
                {
                    if (num <= mas[i])    //System.IndexOutOfRangeException: "Index was outside the bounds of the array."
                    {
                        num = mas[i];
                    }
                }
                mas2[EndMas] = num;
                EndMas--;
                num = -1999999999;
            }
            else
            {
                R = true;
            }
        }
        for (int j = 0; j <= mas.Length; j++)
        {
            mas[j] = mas2[j];
        }
    }
}

Помагити


Ответы (2 шт):

Автор решения: Igor
for (int i = 0; i < EndMas; i++)
                 ^^^ 
...
mas2[--EndMas] = num;
// EndMas--;
→ Ссылка
Автор решения: Maksimka
class Algaritm
{
    public void SortArr(ref int[] mas)
    {
        bool R = false;
        int num = -199999999;
        int EndMas = mas.Length;
        int EndMas2 = mas.Length;
        EndMas2--;
        int[] mas2 = new int[mas.Length];
        int num2 = 0;
        while (!R)
        {
            if (EndMas2 + 1 > 0)
            {
                for (int i = 0; i < EndMas; i++)
                {
                    if (num < mas[i])
                    {
                        num = mas[i];
                        num2 = i;
                    }
                }
                mas2[EndMas2] = num;
                mas[num2] = 0;
                EndMas2--;
                num = -1999999999;
            }
            else
            {
                R = true;
            }
        }
        for (int j = 0; j < mas.Length; j++)
        {
            mas[j] = mas2[j];
        }
    }
}

Вот теперь то все работает)

→ Ссылка