Hi. I have this code:
using System;
class Selection
{
public static void SelectionSort(ref int[] T)
{
int min = 0, i, j, pom;
for (i = 0; i < (T.Length-1); i++)
{
min = i;
for (j = i + 1; j < T.Length; j++)
{
if (T[j] < T[min])
min = j;
}
pom = T[i];
T[i] = T[min];
T[min] = pom;
}
}
public static void InsertionSort(ref int[] T)
{
int j, pom;
for (int i = 1; i < T.Length; i++)
{
j = i;
pom = T[i];
while (T[j-1] > pom)
{
T[j] = T[j-1];
j = j-1;
}
T[j] = pom;
}
}
public static void BubbelSort(ref int[] T)
{
int pom;
for (int i = 1; i < T.Length; i++)
for (int j = T.Length; j > i; j--)
if (T[j-1] > T[j])
{
pom = T[j-1];
T[j-1] = T[j];
T[j] = pom;
}
}
public static void Main()
{
Console.Write("Podaj liczbe el. tablicy: ");
int n = Int32.Parse(Console.ReadLine());
int[] tab = new int[n];
Random rnd = new Random();
for (int i = 0; i < tab.Length; i++)
{
tab[i] = rnd.Next(10);
Console.WriteLine(tab[i]);
}
Console.WriteLine();
BubbelSort(ref tab);
for (int i = 0; i < tab.Length; i++)
Console.WriteLine(tab[i]);
Console.ReadLine();
}
}
And when i am using BubbelSort and InsertionSort program shows exception System.IndexOutOfRangeException, and my question sounds why ?? (SelectionSort works good)