modify your logic like this
int[] items = { 60, 30, 40, 20, 80 };
int temp = 0;
int n = items.Length;
for (int i = 0; i < n; i++)
{
for (int j = i ; j < n - i - 1; j++)
{
if (items[j] > items[j + 1])
{
temp = items[j];
items[j] = items[j + 1];
items[j+1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
Console.WriteLine("values:" + Convert.ToString(items[i]));
}
The reason you are getting exception is you are trying to compare items[4] with items[5] which doesnot exists....
so you need to restrict you termination condition to n-I-1
and also you need to change the initial value of j ...