Is this a proper selection sort?
Any code issues?
public static void SelectionSort(ref int[] a)
{
int n = a.Length;
for (int j = 0; j < n - 1; j++)
{
int iMin = j;
for (int i = j + 1; i < n; i++)
{
if (a[i] < a[iMin])
{
iMin = i;
}
}
if (iMin != j)
{
int temp = a[j];
a[j] = a[iMin];
a[iMin] = temp;
}
}
}
//test
int[] ar = new int[] { 90, 90, 71, 82, 93, 75, 81, 54, 36, 102, 99, 34, -56, 103, 78, 796, 52, -56, 5, 215 };
SelectionSort(ref ar);
foreach (int i in ar)
Debug.WriteLine(i);
refkeyword for theaparameter? Since you just set values of an array it is useless. As for downvote I think it is because of the task itself. Selection sort is one of the first algorithms when people start to learn programming. You doesn't look like a beginner so it is really strange to see this post from you :) Of course there are tons of code examples of this sort on all programming languages including C#. \$\endgroup\$